Generally, the closer you get to a heat source the more heat you receive. The reason it gets colder as you go upwards has nothing to do with how close you are to the Sun. (Five miles up Everest is insignificant when you consider that the Sun is about 93 million miles away).
Instead it has to do with the effect of the atmosphere. Without an atmosphere it would be unbearably hot by day and freezing cold at night. The atmosphere protects us from both extremes by acting as a �sink� for the Sun�s heat, absorbing it and spreading it by day and storing it to maintain a bearable temperature at night.
The atmosphere becomes much thinner quite rapidly with altitude and the atmosphere�s ability to hold on to the heat from the Sun diminishes. At about 7 miles it is about -50 degrees C. However, this reduction is not simply maintained and anomalies occur throughout the various layers of the atmosphere. The temperature picks up again through the stratosphere (up to about 35 miles) drops off through the mesosphere (up to 50 miles) before rising rapidly through the thermosphere (up to about 350 miles). At this altitude the atmosphere is almost non-existent and the Sun�s rays are unfettered, allowing temperatures of 500 degrees plus in the uppermost layer, the Exosphere.