Upgrades are handed out for two broad reasons: either the hotel has to, or the hotel wants to.
Like airlines, hotels overbook: a hotel with 100 rooms will accept (say) 110 bookings, and expect that 10 people don't show up. (For example, my employer's corporate booking rate at a large chain lets you cancel bookings for free until 6 PM on the day of arrival. Do people use this? You betcha.) And since hotels have different types of rooms, each in finite quantities, they'll also accept (say) 55 bookings for their 50 Standard rooms and 55 bookings for their 50 Deluxe rooms. Guess what happens when you're guest #51 to check in for a Standard room? Oops, no rooms left, but the hotel has to put you somewhere: congratulations, you just got an upgrade.
As an additional complication, while you book an airline seat for a single flight, hotel rooms can be booked for any number of nights. So you may get upgraded even if the rooms aren't full right now, because that may avoid a situation a few days down the line.
The second reason is that hotels want to upgrade their best guests. For example, if you're a Starwood Preferred Guest Platinum member (meaning you spend at least 50 nights/year in their hotels), you'll get a free upgrade to the best available room on check-in, up to and including suites. The "on check-in" condition makes this essentially free for the hotel, as they'll only bump you up when they have rooms to spare, but it's obviously a nice perk for the guest. Most major chains have similar plans, eg. Marriott Gold/Platinum get automatic access to the Club lounge and thus often get bumped up to the Club floor as well.