Simple summary, in the lowest atmosphere, the troposphere, the temperature declines as altitude rises, as rising pockets of air lose thermal energy to gain potential energy. In the stratosphere it increases with altitude, due to UV absorption. In the mesosphere it declines again.