Think of the atmosphere as a layer cake. The bottom of the cake is very dense and the higher up you go, these layers get less and less dense. Up on a mountain, the air is very thin.
In other words, there are fewer air molecules per cubic foot (volume of air). The molecules are farther apart and can hold less heat energy. Because "heat" is what we say when we mean molecules are moving around. The more they move, and the more molecules there are, the more heat you have.
It's also helpful to know that the air is heated by the ground and oceans. The heat from our sun mostly passes right through the atmosphere, not directly warming the air up very much. But the surface of the planet will warm up wherever the sun is shining on it. And in turn, the warm ground or the warm surface water then gradually warms the air from the bottom up. (This is because heat is transferred in different modes: radiant, convection, and conduction.)
And the warm air does indeed rise. As it rises, it gradually spreads out and cools off again. Some of the heat even radiates back out into space.
There are "fountains" of air constantly circulating throughout the atmosphere, and this creates weather patterns. It tends to snow on mountains because the warm air has carried some moisture with it on its way up. As it cools and thins, it can't carry the moisture any more, and the moisture precipitates out. Which is why we call it precipitation whenever it snows, rains, sleets, etc.
So by the time air reaches a high mountaintop, it's probably going to be cool or even frigid cold.
This is also why hotter regions, like the southern US, tend to get very humid in the summer. The warm air can carry a lot of moisture, and there is a lot of warm surface water. Our sweat is less efficient when the air is moist, because it takes longer to evaporate and carry the heat away with it.
Deserts have few water sources. So they also have hot dry air, and much less humidity, and therefore little to no precipitation. But they also get cold at night, because there's very little humidity to hold the heat overnight.
All of this is to illustrate the complex interactions between the sun, the atmosphere, and water (or lack of it) on the surface, and humidity in the air.
Inside an older building you're more likely to experience warmer air on higher floors than lower floors because the air is trapped in a nearly closed system and hot air rises. Of course, HVAC engineers try to compensate for this in modern buildings.
While I agree in general, one point is a bit to simplified in my opinion
In other words, there are fewer air molecules per cubic foot (volume of air). The molecules are farther apart and can hold less heat energy. Because "heat" is what we say when we mean molecules are moving around.
Less molecules mean less heat, it has nothing to do with the temperature, if you just decrease the density by removing half the molecules, you have the same temperature.
It cools down because it expands adiabatically. Consider a very thin balloon filled with air which is warmer than the surrounding. This now rises up, but as it does, the pressure decreases, causing the balloon to expand. During this expansion, the balloon transfers energy away from itself, because it has to push away air, to make room for expanding in the surrounding. This work cools the air inside the balloon. Assuming the air inside is dry, it would cool around 10 °C per km it rises.
Now if you think about it, the balloon just stopped the inside from mixing with the outside. If you look at a large "piece" of air, it does not mix very fast, so you can remove the balloon and just consider what happens with warm air heated from the ground.
Now this does not mean, it has to be cooler when higher up. The same points hold, inside a house, but there it is often warmer when higher.
The best explaination is when looking where the heat comes from and goes too from the air. The atmosphere is mostly heated from the surface of earth, so the bottom and cooled from the upper layers. So naturally it gets hotter where it is heated. The question is now by how much? There are three modes of heat transfer in the atmosphere: radiation, conduction and convection. The first two are very slow. Connection is fast but has limits. Consider the piece of air, if it rises, it cools. So at some place it may be the same temperature as the surrounding air, so it stops rising. This means the convection works only when the air gets cooler by 10 °C/km going up (~6.5°C when the air is moist and precipation happens). So this temperature gradient is observable very often.
Others have covered the fact it's because of air pressure but haven't fully answered why that is the way it is.
It's simple really.
The force of gravity is also at play. As you go higher up, gravity gets weaker as you get farther from the earth's centre.
And it is that gravitational force that increases the air's density, same reason why if you keep going down in the water, the water gets denser.
For the heat to move around you need to be in a sort of goldilocks zone of density.
It needs to be dense enough that the fluid molecules can move around and spread the convection energy around... but not so dense they can't move much either.
Furthermore there's actually a couple different layers of our atmosphere.
First at our level is the troposphere, where heat is absorbed into the ground itself and radiated back out, as well as the perpetual heat from the earth's core, and reflected off the ground too (visible light).
The troposphere is warm and gets colder as you get farther away from the earth's surface, naturally. That heat is absorbed by the air itself so, as you get farther away it gets colder as it has more air to travel through.
Up higher is the Stratosphere, where it's ice cold and the air thins out.
However we get a sudden uptick in temp as we go even higher into what is called the Stratopause, back to briefly warm temperatures between the Stratosphere and the Mesosohere. Why? How?
Simple, this is the little sweet spot Ozone molecules hang out, forming a protective convenient bubble around the earth. Ozone absorbs Ultraviolet light from the sun and turns out that stuff is HOT, so there's a band of a hot zone right above and below the Ozone layer. Think of it as a toasty little bubble around us.
Above is the mesosphere which cools off again and gets back to being really frosty quickly, for the same reason the Stratosphere did, distance.
Then we hit the mesosphere, which is effectively the point when the atmosphere is so thin it stops protecting and is the "outside" of our protective blanket.
You can imagine this like earth being wrapped in a blanket, and the mesosphere is everything outside the blanket. Without any protection you are subject to the unbridled radiation of the sun which means you go back to being really toasty, as you get a bit higher you are effectively in space now and will soon enough hit temps that just cook you alive in a minute or two. Really bad sunburn zone.
So to answer the question overall:
Hot air rises... but only when there is air to rise.
Top of the mountains just don't have enough air anymore for it to really rise much more. It still does but the hot air rising effect just gets weaker and weaker as the air gets thinner due to less gravity.
The strength of the gravity field at the ISS orbit (400km) is still 90% of what it is at sea level. The air thins out at high altitudes because there's less air above it pushing down, not because of weaker gravity.
To add to this, the force of gravity at the top of Mt Everest is about 99.7% as strong as sea level. So you're right that it's not about the strength of gravity itself at that particular point, but about the weight of all the air above that point.
I saw a great one-liner, and two megalogs, but no Goldilocks-sized answer, so here's my attempt.
As air rises, the weight of air above it (all the way to space) is less, so it's less squashed, letting it expand.
It expands by pushing out on all the air around it, and every time an air molecule bumps a neighbouring bit of air away, but isn't bumped back so hard (so it expands), it loses a bit of energy - i.e. heat.
So as some air goes up, it expands and loses heat; or as it sinks, it squashes and gets more heat.
This is adiabatic expansion.
Appendix:
This might beg the question of why higher air isn't just heated by neighbouring expanding air, making up for its original loss. I think that can be answered by saying overall the top air is squashing the bottom air, so overall the top is cooler. Is that fully right? Right now I feel there's multiple ways to think about it and I can't write any clearly without long rambling!
The temperature of space actually is close to absolute zero, so quite cold. As the heat balance of an object there is mainly dominated by radiation, the object looses heat (~T⁴) but almost has no heat input from the surrounding if not directed to a star in sufficient proximity, e.g. the sun. The surface exposed to sunlight however, can become really hot.
It really depends on what you mean by temperature. You're both right, but both wrong depending on context.
Individual atoms and particles tend to have a lot of energy, but also there's almost no heat transfer into larger bodies because of the low density of those particles, so you lose more heat to radiation than you take in (unless you are in direct sunlight.)
Without checking, I would say that it's because the heat dissipate away from the planet and the hot air will eventually cool down while rising? My understanding is that it's hot near sea level because it's where the heat from the sun gets reflected and radiated from the earth surface, correct me if I'm wrong...
I believe it's less about heat dissipation than about adiabatic expansion - where air as it expands does 'work' and loses heat.
The heat coming (as far as the air is concerned) mainly from the ground sounds like a good point, but IIRC the temperature at altitude follows the expected curve for adiabatic expansion given the pressure change, so I think that heat-entry-point-effect must be much less significant except close to the ground.
Come to think of it, most heat loss from the earth must be from infra red, which will also come from the opaque ground much more than from the air.
Yeah "dissipate away" is probably a bit misleading but I meant that the heat source is mainly the surface since it's difficult to heat the thin outer layers directly, and from there heat moves up thorough ir radiation or adiabatic expansion. But it's not like mountains are cooled down by adiabatic expansion, since the air wouldn't move up without a temperature gradient, which means that it cannot get colder that the mountains already are. So I would think they are simply farther away form surface heat radiation and have thinner air that don't assorb heat...
While it's true that hot air rises, causing lower temperatures at higher altitudes, the reason it's colder on top of mountains is due to the decrease in air pressure with altitude. As air pressure decreases, so does the temperature, leading to colder conditions at higher elevations. Additionally, factors such as exposure to winds and proximity to polar regions can further contribute to colder temperatures on mountain peaks.
The stratosphere is heated not by the ground but directly from solar radiation leading to higher power input at the upper layers. So the top is hotter and now convection never starts and you get no cooling of the air when rising.