It might be common sense to make sure you have a hotel reservation before arriving in a city, but unfortunately that can't always be the case. Personally, I've had to sleep in bus stations and airports when I was younger but I never really thought about it much. However, last week I met a guy wandering around the streets of San Diego looking for a hotel room without any luck (it was Comic Con and there simply were no rooms available).
That spurred my thinking to come up with cheap, free, and nearly free ways that you could safely spend the night in a strange city - even with no hotel reservations.
24-hour Subway or Starbucks
This is probably the cheapest solution - simply go and order a sandwich or coffee every hour or so and you'll be set. I would extend this to doing the same at a Burger King or McDonalds too, but those tend to be a bit sketchier and night compared to a Subway or a Starbucks.
Rent a Car for the Night
Renting a car instead of a hotel room is probably the best idea if you are coming into town via an airport. However even if you are taking a bus or train, you can still typically get to an airport pretty easily and rent the car there. While the most expensive of the options at about $50-$100 this is probably the best solution since you'll have both privacy and security. Simply park it in a well lit parking spot such as a 24-hour Walmart for instance. That way you can also get food and use the restroom if you need it.
Public Train / Bus
This is a cliche and you'll probably be looked at as a homeless person if you do this but it can work. That being said, I don't recommend this option since trains tend to be unmonitored and at night can be scary. It is free though.
Train / Bus Station
This is a bit safer than riding a bus or train and you are less likely to get kicked out when you fall asleep. It's also a free option that has bathrooms and usually a place to eat.
Check into a Hostel
Assuming you are able to start your search early in the day or early evening, staying at a hostel is a good cheap alternative to staying in a hotel as well. You can find hostels by looking online at sites like hostels.com or Hosteling International. Typically you'll find that the price of a night at a hostel is < $50 even in cities like Chicago and San Diego. However, you'll usually sacrifice privacy and other amenities and may be asked to contribute to the hostel by cleaning something or offering other services. You'll also usually have to share a room and bathroom ... though many do offer private rooms for a much higher rate.
Couchsurfing App
This won't work well if you are stranded without a room, but if you have a little time to prepare it's a great idea. Simply login and connect with people who have a couch or spare bed available. Successfully pairing with a Couchsurfing host needs some time to execute but it's a good solution if you are willing to roll the dice and meet new people.
I'm sure there are more ways to send the night in a new city when you can't afford or don't have a hotel reservation, so I'd love to hear your ideas well.