...And are determined by a number of factors
Including the flight's direction, turbulence, and flight duration. For example, there's “the cardinal direction altitude rule.” This stipulates that commercial airliners traveling Northeast, East, Southeast, and one degree short of South fly at odd altitudes (31,000ft, 33,000ft, etc.), while those traveling all other directions fly at even thousands (32,000, 34,000ft, etc.).
Clear-air turbulence (CAT) affects altitude, as well. Pilots report any turbulence they encounter, and Air Traffic Control (ATC) uses that information to steer other planes above or below it.
Finally, longer flights benefit from flying at higher altitudes; the thinner air reduces drag, increases engine efficiency, and saves fuel.
Airline dispatchers calculate the ideal altitude for every flight
Airline dispatchers are employed by carriers, and generally work in the company’s HQ. They control the planning and execution of their airline’s flights -- keeping track of where they are, where they are going, their fuel/weight, as well as assigning them specific routes. Most importantly, they calculate a flight’s ideal altitude and request it from Air Traffic Control, who tries to accommodate the request when giving the pilot clearance to take off.
There are highways in the sky
They’re called jet routes, and ATC uses them to separate planes by a certain distance or altitude (generally around 1,000ft vertically) and adjusts them to control traffic flow.
Despite a ton of sky, however, "traffic jams" still happen. For example, when thunderstorms build up there can be hundreds of miles with only one or two suitable crossing points at specific altitudes. It's up to ATC to steer all the traffic through those limited windows.