The sky is blue because of a process called Rayleigh scattering. This is where electromagnetic radiation passing through a molecule significantly smaller than the wavelength of the radiation causes polarisation in the molecule, the effect of which is to scatter the radiation. Crucially, this scattering is proportional to the wavelength - smaller wavelengths are scattered much, much more than larger ones. Blue light has a shorter wavelength than red or green light, so for sunlight passing through the earth’s atmosphere blue light is scattered significantly more than any other colour - away from the direct path between the sun and your eye (or a camera lens). This makes the sky look blue. In the direction of the sun, and in its immediate vicinity, the sky takes on a yellowish colour, because much of the blue light is scattered away.
Figure 1: Diagram showing how Rayleigh scattering gives the sky its blue colour. From http://astronomy.nju.edu.cn/~lixd/GA/AT4/AT407/HTML/MP0701.htm
The same process leads to the red colour of the sky during sunsets. When the sun close to the horizon, sunlight has to pass through more air to reach the ground than when the sun is high in the sky. This means more wavelengths are scattered away, leaving behind mainly red light around the sun, as red light has the longest wavelength of any visible light. If it weren’t for Rayleigh scattering, we would be able to look up at the sky in full daylight, and see the stars. This is the case on the Moon, where there is no atmosphere. On Mars, conditions are a little different; the Martian sky is a pinkish, orange colour because of the abundance of iron (III) oxide dust in the atmosphere which is reddish in colour. The atmosphere itself is too thin for Rayleigh scattering to affect the colour of the whole sky; however the Martian sunset has been seen to have a subtle, blue halo around it, which has been attributed to Rayleigh scattering.
Figures 2&3: Images of the martian sky in daylight and at sunset. Images from NASA Mars Pathfinder.
References and Further Reading: