Because the sun’s white light is made up of a rainbow spectrum of colours, and the short wavelength of the blue light means it gets scattered by gases in the Earth’s atmosphere.
Light is made up of waves of energy, and the wavelength differs for different colours (as you can see when sunlight refracts through water droplets to create a rainbow).
Blue light has a short wavelength, which means it gets scattered and deflected more than colours with longer wavelengths – like red – when it reaches the gas molecules in the Earth’s atmosphere.
So, a blue sky is sunlight which has been deflected by atmospheric gases before reaching your eye.
At sunset, when sunlight passes through the atmosphere at an angle to the point where it is seen (and therefore passes through more gas) the blue light has been scattered and deflected so much that less of it reaches your eye.
So, the Met Office explains, there is “relatively more yellow and red light left for us to see”.
In space, where there is no gas to scatter the sunlight, you see the sun with no blue sky around it.
Therefore, in the movie Spaceballs, when President Skroob tries to steal planet Druidia’s atmosphere with a giant hoover, the sky should’ve started turning black.
But maybe that was scarier than the movie they were trying to make.