tl;dr: Blue light has a shorter, smaller wavelength causing it to be scattered more throughout the atmosphere than other colors.
Explanation:
Sunlight is a portion of the electromagnetic radiation given off by the Sun, typically showing up as white to the human eye, as it’s a mixture of all the colors of the rainbow. As the sunlight passes through the atmosphere, it causes charged particles inside air molecules to oscillate up and down. Air particles are then scattered in all different directions.
The blue component of the spectrum of visible light has shorter wavelengths and higher frequencies. As the sunlight that consists of all colors passes through the air, the blue wavelengths cause charged particles to oscillate faster than other colors. The faster the oscillation, the more scattered light is produced; therefore, the blue is scattered more strongly than other colors.
Interestingly enough, violet light is scattered even more strongly than blue; however, our eyes are more sensitive to blue light than violet light, therefore the sky appears blue.
“But then why do we see reds during sunrise/sunset?”
During sunset and sunrise, the opposite phenomenon occurs. We see only the light that has not been scattered into other directions. It is the red wavelengths of sunlight that have not scattered as much as the blue wavelengths that we see. This is because there are more opportunities for blue light to be scattered when the sun is overhead than when it is on the horizon.
“But why isn’t the sky blue on the Moon?”
The scattering occurs by interacting with the particles in the atmosphere. A planet with no atmosphere cannot have a colorful sky.