It will depend on the dealership. Some dealerships will allow you to take a car for an extended test drive for a few hours or overnight if they know you're serious about buying it. Plan to take a couple of short test drives first – a dealership probably won't let you take the car for a longer period if you haven't tested it at all yet. Once you've narrowed down your options to one or two vehicles, ask about an extended test drive.
When looking at a car from a private seller, the owner can decide if they're comfortable allowing an extended or overnight test drive. As a security measure for both parties, the seller may ask you to sign something that defines the test drive terms.