When light travels from one medium to another, it undergoes refraction and hence its path changes. Similarly in our atmosphere also there are various layers of air (denser and rarer) due to which their refractive indices are also different. All the stars that we see are very far away from us and therefore they can be considered as point like sources of light. So when the light from the stars enter the atmosphere they pass through denser and rarer mediums hence undergoing refraction continuously. As a result of this refraction the apparent positions of the stars are changing continuously and it seems that the stars are twinkling.