You might have seen the apparent random wavering or flickering of objects viewed through a turbulent stream of the hot air rising above a fire or a radiator. The air just above the fire turns hotter than the air located further up. The hotter air is less dense (lighter) than ,the cooler air above it and has a slightly less refractive index than the cooler air.
Considering the physical conditions of the refracting medium (air) are not stationary, the object's apparent position fluctuates as seen through the hot air. This wavering is thus an impact of atmospheric refraction (refraction of light rays by the earth's atmosphere) on a small scale in our local ecosystem. The twinkling of stars is a related phenomenon on a much larger scale.
Let us see how we can describe it.
Twinkling of stars:
The twinkling of a star is because of the atmospheric refraction of starlight. On entering the earth's atmosphere, the starlight experiences refraction continuously before it reaches the earth. The atmospheric refraction happens in a medium of continuously changing refractive index. Since the starlight is bent by the atmosphere towards the normal, its apparent position is slightly different from its actual position. When viewed near the horizon, the star appears slightly higher (above) than its actual position (as shown in the figure).
Apparent star position due to atmospheric refraction
Moreover, this apparent position of the star is not stable but continuously varying somewhat as the physical phenomenon of the earth's atmosphere are not stable, as was the case in the previous paragraph. Since the stars are very different, they approximate point-sized sources of light. As the pathway of rays of light coming from the star goes on varying slightly, the star's apparent position varies, and the amount of starlight entering the eye flickers – the star sometimes seems to be brighter and at some other time fainter, which is the twinkling effect.
Why don't the planets twinkle?
The planets are greatly closer to the earth and are thus viewed as extended sources.
Let's consider a planet as a group of a large number of point-sized sources of light. The total variation in the amount of light piercing our eye from all the respective point-sized sources will equate out to zero, thereby nullifying the twinkling effect.