Whenever I'm travelling by road on a hot sunny day and I look towards the horizon, it appears as though there is puddle ahead but as I approach it, it disappears. Why does this happen?
This optical illusion is due to refraction of visible light due to density gradients near the surface and the fact that the index of refraction in a medium is dependent on the density of the medium. In an atmospheric density gradient light will bend toward the direction of increasing density. When it is cool at the ground and warms up with height this effect bends light toward the ground and when it is hot near the ground and cools with height this bends light toward the sky. This is particularly strong over a road surface, which can be much hotter than the surrounding land surface. This produces a sensible heat flux that warms the air over the road and can produce exceptional temperature gradients near the surface.
This strong temperature gradient near the ground is also a significant density gradient and when you look through it toward the horizon you are actually seeing the sky. Photons that would normally illuminate the surface of the road are instead refracted by the density gradient and are bent toward your eye. This only works for specific angles determined by the actual density gradient and this is why the mirage always appears at a certain distance. You never really get closer or further from the "water" and you may be able to see road surface beyond the illusion.
This illustrates a the effect of a near-surface density gradient that is low density (warm) at the ground and increases (gets colder) with height. The photons from the sky that pass close to the road are refract toward the observer and you see a false image of the sky as an inferior mirage. The other photons do not pass through the strong density gradient and are not bent and you see the true positions of the objects.