This is an effect of Rayleigh scattering. When photons (light) crosses the atmosphere, some
of the photons will be absorbed by gas molecules. This puts the molecule in an excited
state, and it is then free to drop down to ground state again and release the energy in the
form of another photon.
To excite the molecule, you must do so with a photon at or near it's resonant frequency. It
so happens that the resonant frequency of the gas molecules in the atmosphere is in the
purple-blue part of the visible spectrum. This means that it will absorb and scatter much of
the blue light contained in the sun's rays, green to a lesser degree, yellow to a lesser
degree and red to a lesser still degree.
This is why the sky is blue; some of the blue light coming from the sun is scattered
laterally by the gas molecules in the atmosphere. We see the blue light coming at us from
all directions in the sky.
Sunsets are reddish because the sun is not directly overhead and it's rays must cross
through much more atmosphere than the midday sun. After having crossed so much air, most of
the blue light is scattered out, as well as most of the green. This leaves the red, yellow
and orange colors free to paint their pictures of fiery sunsets and hazy moons.
Answered by: Jesse Clair, Physics Undergrad, Mount Allison U, Sackville, NB