Anthony D. Del Genio of the NASA Goddard Institute for Space Studies and Columbia University explains.

To understand why the sky is blue, we need to consider the nature of sunlight and how it interacts with the gas molecules that make up our atmosphere. Sunlight, which appears white to the human eye, is a mixture of all the colors of the rainbow. For many purposes, sunlight can be thought of as an electromagnetic wave that causes the charged particles (electrons and protons) inside air molecules to oscillate up and down as the sunlight passes through the atmosphere. When this happens, the oscillating charges produce electromagnetic radiation at the same frequency as the incoming sunlight, but spread over all different directions. This redirecting of incoming sunlight by air molecules is called scattering.

The blue component of the spectrum of visible light has shorter wavelengths and higher frequencies than the red component. Thus, as sunlight of all colors passes through air, the blue part causes charged particles to oscillate faster than does the red part. The faster the oscillation, the more scattered light is produced, so blue is scattered more strongly than red. For particles such as air molecules that are much smaller than the wavelengths of visible light the difference is dramatic. The acceleration of the charged particles is proportional to the square of the frequency, and the intensity of scattered light is proportional to the square of this acceleration. Scattered light intensity is therefore proportional to the fourth power of frequency. The result is that blue light is scattered into other directions almost 10 times as efficiently as red light.

When we look at an arbitrary point in the sky, away from the sun, we see only the light that was redirected by the atmosphere into our line of sight. Because that occurs much more often for blue light than for red, the sky appears blue. Violet light is actually scattered even a bit more strongly than blue. More of the sunlight entering the atmosphere is blue than violet, however, and our eyes are somewhat more sensitive to blue light than to violet light, so the sky appears blue.

When we view the setting sun on the horizon, the opposite occurs. We see only the light that has not been scattered into other directions. The red wavelengths of sunlight that pass through the atmosphere without being scattered much reach our eyes, while the strongly scattered blue light does not. The longer distance that the sunlight travels through the atmosphere when it is on the horizon amplifies the effect--there are more opportunities for blue light to be scattered than when the sun is overhead. Thus, the setting sun appears reddish. In a polluted sky, small aerosol particles of sulfate, organic carbon, or mineral dust further amplify the scattering of blue light, making sunsets in polluted conditions sometimes spectacular.

Clouds, on the other hand, are made of water droplets that are much larger than the wavelengths of visible light. The way they scatter sunlight is determined by how the light is refracted and internally reflected by, and diffracted around, the cloud droplets. For these particles the difference between the scattering of blue and red light is not nearly so large as it is for gas molecules. Hence, our eyes receive substantial scattered light at all visible wavelengths, causing clouds to appear more white than blue, especially when viewed against a blue sky background.

Since scattering by the atmosphere causes the sky to be blue, a planet with no atmosphere cannot have a bright sky. For example, photographs taken by the Apollo astronauts on the moon show them and the moon's surface bathed in sunlight, but a completely dark sky in all directions away from the sun.