At higher altitudes, the air becomes rarer. Also the density is not constant. There are variations in the air densities all the time. The reasons for this are wind, heat, moisture and numerous other factors. While traversing from material mediums with varying densities, light rays undergo refraction.
Refraction is nothing but bending of the light rays.
It actually means that the light rays have different speeds in denser and rarer mediums. This effect changes the direction of the light rays. The stars are such a great distance that they form a point source of light. The light rays coming from the star, due to refraction in the atmosphere, sometimes reach our eyes and sometimes they do not. So it appears as though the light from the star is switching on and off.