A larger amount of gas can dissolve in a liquid when it is cold than when it is hot. Or as a chemist would say, the solubility of a gas in a liquid increases with decreasing temperature. But that’s how chemists talk.
In practical terms, why does the carbon dioxide choose to leave the beer just because it is getting warmer? From everyday experience, you might expect that as the liquid gets warmer, it should be capable of dissolving more stuff, not less. You can dissolve more sugar in hot tea than in iced tea, can’t you? Then why should gases be any different?
The answer lies in the role that heat plays in the dissolving process. It can be a very complicated one.
When a substance dissolves in water, its molecules separate from one another and disperse themselves throughout the water. Other changes may occur at the same time, depending on the substance that is dissolving. For example, the molecules might attach themselves to tight little clusters of water molecules, or they may react chemically with the water, or they may split into electrically charged fragments, or do other things too horrible to contemplate.
All of these processes either use up or give off energy in the form of heat. So heat plays an intimate, and widely varying, role in the dissolving of various substances. The net result is that some substances will eagerly absorb the extra heat in hot water and use it to dissolve more, while some substances will react negatively to the extra heat and dissolve less. In other words, some substances will be more soluble in hot water than in cold, and others will be less soluble in hot water than in cold. Even chemists can’t always predict which way it will go for a given substance.
In the case of gases, though, we can make a sweeping generalization: When gases dissolve in water, they all give off energy in the form of heat. You could say (and I will) that dissolving gases don’t like heat; they’re trying to get rid of it. So they will dissolve more readily in a cold, heat-absorbing environment like cold water, and they are discouraged from dissolving in a hot, heat-rich environment like hot water.
Let a glass of cold water stand around for a few hours and, as it warms up, you’ll see bubbles of air forming on the walls of the glass. The air was dissolved in the cold water, but the warmer water can’t retain that much air. It “goes flat,” just like beer.