This phenomenon is called Rayleigh scattering.
when light from the sun hits particles in the atmosphere, the electrons of these particles are going threw excition to a higher level, and then dropping back to the ground level, while emitting radiation - visible light.
The Rayleigh scattering is stronger when the wavelength is shorter,so blue light is scattered a lot more than red light, this is why you see blue sky.
during surise/sunset the light goes through much greater thickness of the atmosphere that causes multiple scatterings of almost only blue light,and a little of red light.