How can air be warmer high in the atmosphere?

Dear Tom,
You say the anvil tops of thunderstorms are formed when the rising column of warm, moist air in the storm finally encounters air warmer than itself. But we always hear that temperatures drop 3.5 degrees for each 1,000 feet of altitude gained. How can higher air be warmer?
— John Podulka, Winfield
Dear John,
The atmosphere is divided into four layers. The lowermost layer, the troposphere, extends from the surface upward about 6 to 10 miles (depending on location and time of year). This is the layer in which “weather” occurs and in which temperatures decrease about 3.5 degrees per thousand feet of height gain. Upward from there is the stratosphere, in which temperatures increase to a height about 31 miles aloft. The mesosphere is next, a layer from about 31 to 50 miles up, in which temperatures again fall with increasing altitude. Above that is the thermosphere, a zone of very high temperatures.