Cold air is denser than warm air and has therefore a greater refractive index. As light passes from colder air across a sharp boundary to significantly warmer air, the light rays bend away from the direction of the temperature gradient (the "normal" in the figure on the left). When light rays pass from hotter to colder, they bend toward the direction of the gradient. The diagram on the left shows a light ray coming from the sky toward the hot ground. If the air near the ground is warmer than that higher up, the light ray bends in a concave, upward trajectory.
Once the ray reaches the viewer’s eye, the visual cortex interprets it as if it traces back along a perfectly straight "line of sight". This line is however at a tangent to the path the ray takes at the point it reaches the eye. The result is that an "inferior image" of the sky above appears on the ground. The viewer may incorrectly interpret this sight as water which is reflecting the sky, which is, to the brain, a more reasonable and common occurrence.
In the case where the air near the ground is cooler than that higher up, the light rays curve downward, producing a "superior image".
The "resting" state of the Earth's atmosphere has a vertical gradient of about -1° Celsius per 100 metres of altitude. (The value is negative because it gets colder as you go higher up.) For a mirage to happen, the temperature gradient has to be much greater than that. According to Minnaert, the magnitude of the gradient needs to be at least 2 °C per meter, and the mirage does not get strong until the magnitude reaches 4º or 5 °C per meter. These conditions do occur when there is strong heating at ground level, for example when the sun has been shining on sand or asphalt and an inferior image is commonly generated because of this.