Light travels in waves, similar to waves on the ocean. The science term for light waves is electromagnetic waves. Like waves on the ocean, electromagnetic waves can be measured by their wavelength. This is the length between two crests in the wave. A crest is the top part of the wave (the foamy white part). Light coming from the sun is made up of all the colors of the rainbow and is known as white light. All the colors in white light have different wavelengths. Red light has the longest wavelength of all the colors. This wavelength is larger than the oxygen atoms in the atmosphere. When red light passes through the atmosphere, its long wavelength causes it to pass through the oxygen atoms without being scattered or spread around. Blue light on the other hand has a much shorter wavelength than red light. The wavelength of blue light is smaller than the oxygen atoms and when blue lights passes through the atmosphere it collides with the oxygen atoms. These collisions cause the blue light to be scattered in all directions. This scattering of the blue light is what causes the sky to appear blue. All other colors, with longer wavelengths than blue, are scattered as well, but blue light's short wavelength causes it to be scattered the most. Actually violet light has the shortest wavelength of all the colors and is scattered even more than blue light, but our eyes our much more sensitive to blue light so we see the sky as blue.
During a sunset, the white light has to travel through much more atmosphere than when the sun is directly overhead. The blue light and other short wavelength colors are scattered around and diluted so much by this that only the longer wavelengths of light remain. This is why the sky appears red, orange, and yellow during sunsets.
Information to answer this question was found at http://www.why-is-the-sky-blue.org/