The sun produces white light, which is made up of light of all colors: red, orange, yellow, green, blue, and violet.
Light is a wave, and each of these colors corresponds to a different frequency and therefore a different wavelength of light.
The colors in the rainbow spectrum are arranged according to their frequencies: Violet and blue light have higher frequencies than yellow, orange, and red light.
When the white light from the sun shines through the earth’s atmosphere, it collides with gas molecules. These molecules scatter the light. The shorter the wavelength of the light, the more it is scattered by the atmosphere. Because its wavelength is so much shorter, blue light is scattered approximately ten times more than red light.
In addition, the frequency of blue light, compared to red light, is closer to the resonant frequency of the atoms and molecules that make up the air. That is, if the electrons bound to molecules in the air are pushed, they will oscillate with a natural frequency that is even higher than the frequency of blue light. Blue light pushes on the electrons with a frequency that is close to their natural resonant frequency, which causes the blue light to be re-radiated out in all directions in a process called scattering. The red light that is not scattered continues on in its original direction. When you look up in the sky, the scattered blue light is the light that you see.
Why does the setting sun look reddish orange? When the sun is on the horizon, its light takes a longer path through the atmosphere to your eyes than when the sun is directly overhead. By the time the light of the setting sun reaches your eyes, most of the colors of light have been scattered out. The light you finally see is reddish orange.
Violet light has an even shorter wavelength than blue light: It scatters even more than blue light does. So why isn’t the sky violet? Because there’s just not enough of it. The sun puts out much more blue light than violet light, so most of the scattered light in the sky is blue.