The direct answer is that this phenomenon is due to the scattering of light in Earth's atmosphere. When the sun is low in the sky (sunrise or sunset), its light travels through more atmosphere before reaching our eyes, which intensifies the scattering of short wavelengths (such as blue), while long wavelengths (such as red) are scattered less and dominate on the horizon.
This effect is called Rayleigh scattering, discovered by Lord Rayleigh in the 19th century. Small particles in the air, such as nitrogen and oxygen molecules, deflect blue light more than red light. During sunset, the sun is close to the horizon, so its light passes through a thicker layer of atmosphere, filtering out almost all the blue and leaving red visible.
Another factor is the temperature of the atmosphere. At sunset, the air near the ground is warmer, which can absorb more blue light, reinforcing the red tone. At sunrise, on the other hand, sunlight has not yet traveled through as much atmosphere, so blue is more visible.
This effect isn't just a curious aspect of the sky—it also explains why sunlight in space (outside the atmosphere) is white, since there is no scattering of light waves there.

