The twinkling of the stars is because the light that comes from them is distorted by our planet's atmosphere before it reaches our eyes. As they are very far away and we only see small points of light, this distortion occurs, which is also accentuated the closer an observer on Earth looks towards the horizon.
In general, any light that crosses our atmosphere suffers the consequences of atmospheric turbulence, being the stars more sensitive to this phenomenon due to their incredible distance compared to the planets, whose light does not "flicker".
So, it could be say this phenomenon is a kind of optical illusion because what we really see is a continuous change of position of the star in a tiny region of the sky, which is what makes us perceive that twinkling.