When a ray of light travels from one medium to another it ‘bends’. This phenomenon is referred to as refraction.
If it travels from a rare medium to a dense medium, it bends towards the normal and if it travels from a dense medium to a rarer medium, it bends away from the normal.
The speed at which the light travels changes depending on the medium and therefore this bending occurs.
The atmosphere of the earth is made of different layers. It is affected by winds, varying temperatures, and different densities as well.
When light from a distant source (a star) passes through our turbulent (moving air) atmosphere, it undergoes refraction many times.
When we finally perceive this light from a star, it appears to be twinkling. This is because some light rays reach us directly, and some bend away from and toward us. It happens so fast that it gives a twinkling effect.
If you are in a large empty field such that the horizon is visible to you, you will notice that stars in this region appear to twinkle a lot more than stars right overhead.