The atmosphere consists of a number of parallel layers of air of varying densities and different refractive indices. The variation in densities of different layers is due to different temperatures and pressures.The density and refractive index of different layers goes on decreasing as we move to higher altitudes.
Light coming from the stars travels through the many layers of the Earth's atmosphere and therefore it suffers multiple refractions. As a result, the light of the star is bent (refracted) many times and in random directions (light is bent when it hits a change in density - like a pocket of cold air or hot air). This random refraction results in the star twinkling out (it looks as though the star moves a bit, and our eye interprets this as twinkling).