To explain why the sky has a color, we first need to know what sunlight is, because the answer turns out to be that it is not one thing. Sunlight looks plain white. But white is what we see when light of every color arrives together, traveling as a single beam.
Isaac Newton tested this carefully. In the 1660s and 1670s he let a thin beam of sunlight through a glass prism and saw it spread into a band of colors, red at one end and violet at the other. Many people assumed the glass was painting the light. Newton showed it was not: he sent one color from that band through a second prism and it stayed the same color, and he showed that the separated colors, brought back together, made white light again. The prism had not made the colors. It had sorted colors that were already in the beam.
The sorting works because glass bends each color by a slightly different amount: violet the most, red the least. The spread-out band is the same ordering you see in a rainbow, where raindrops do the job of the prism: red, orange, yellow, green, blue, violet.
There is a second way to see that white is a team of colors, and it comes with a trap. Shine a red, a green and a blue torch on the same patch of a white wall and where they overlap the wall looks white, because the lights add together. Paint behaves the opposite way. Paint works by absorbing some colors, so mixing paints removes more and more light and gives a muddy brown. Light adds up; paint takes away.
One more fact will matter later. Each color of light is a wave, and the colors differ in their wavelength, the distance from one wave crest to the next. Visible light runs from roughly 400 nanometers (violet) to roughly 700 nanometers (red), where a nanometer is a billionth of a meter. Blue sits near 450. Remember that red waves are long and blue waves are short.