From here:
In Lectures of physics, vol 1 by Feynman, it is written:
"Suppose that the piston moves inward, so that the atoms are slowly compressed into a smaller space. What happens when an atom hits the moving piston? Evidently it picks up speed from the collision. [...] So the atoms are "hotter" when they come away from the piston than they were before they struck it. Therefore all the atoms which are in the vessel will have picked up speed. This means that when we compress a gas slowly, the temperature of the gas increases."
Obviously, if the gas under pressure is not in a perfectly heat-insulated container, it will cool down again/warm its surroundings. Which is why Boyle had to wait for his compressed gases to cool down again before observing that pressure is proportional to volume; the immediate reading showed a higher pressure/temperature. Taking the atmosphere as a whole, it is in fact a self-contained heat-insulated container which contains itself. Gravity does the 'work' holding it in and the vacuum outside is to all intents and purposes a perfect heat insulator*.
The reverse is also true, if the volume of a sealed container with gas in it is increased, the gas will cool (until it is warmed up again by the non-insulating container).
See for example how fridges work:
A vapor compression cycle is used in most household refrigerators, refrigerator-freezers and freezers.
In this cycle, a circulating refrigerant such as R134a enters a compressor as low-pressure vapor at or slightly below the temperature of the refrigerator interior.The vapor is compressed and exits the compressor as high-pressure superheated vapor.
The superheated vapor travels under pressure through coils or tubes that make up the condenser; the coils or tubes are passively cooled by exposure to air in the room. The condenser cools the vapor, which liquefies. As the refrigerant leaves the condenser, it is still under pressure but is now only slightly above room temperature.
This liquid refrigerant is forced through a metering or throttling device, also known as an expansion valve (essentially a pin-hole sized constriction in the tubing) to an area of much lower pressure.The sudden decrease in pressure results in explosive-like flash evaporation of a portion (typically about half) of the liquid. The latent heat absorbed by this flash evaporation is drawn mostly from adjacent still-liquid refrigerant, a phenomenon known as auto-refrigeration.
Clearly, there is an added kicker here, the latent heat absorbed when the compressed gas (liquid) boils/evaporates again, but the principle stands. Heat coming out of the back of the fridge is equal and opposite to the fall in temperature inside the fridge (ignoring the extra bit of heat generated by friction).
See also how stars form:
Gravity pulls the dust and gas together.
As the gas falls together, it gets hot. A star forms when it is hot enough for nuclear reactions to start. This releases energy, and keeps the star hot.
Where does heat come from? From gravity compressing the hydrogen atoms/molecules. Luckily, the earth does not get anywhere hot enough to trigger nuclear reactions!
* Mombers adds:
Big hole in your analysis I think:
'the vacuum outside is to all intents and purposes a perfect heat insulator'
The vacuum provides no insulation for the radiant heat, which is why nights are colder than days.
Greenhouse gases on the other hand do provide insulation for radiant heat...
1. Yes, fair point, heat radiates from the earth equal and opposite to what comes in from the Sun. But let us rule the Sun out of this equation. There was no Sun shining on the earliest clouds of hydrogen, but nonetheless, they heated up, the heat did not radiate out into space or else they would never have ignited. Deny that if you will. I am talking about a specific phenomenon that is independent of heat from the Sun.
2. The vacuum provides the same (lack of) insulation in daytime and night time. As far as I am aware, the reason it is colder at night is because the Sun is not shining on that part of the earth. The relative difference between temperature "where the Sun is shining" and "where it isn't shining" is a separate topic (and easily explained) to "why is it warmer at ground level than at higher altitudes (for a given surface temperature)".
3. Of course greenhouse gases i.e. clouds of H2O vapour reflect radiant heat. Everybody can notice that when it is cloudy at night it is surprisingly warm. That is quite a separate topic to "why is it warmer at ground level than at higher altitudes (for a given surface temperature)". Clouds at low elevation (fog) are warmer than clouds higher up.
