Atmospheric Refraction
The refraction of light by the earth's atmosphere is called atmospheric refraction. The atmosphere is made of layers of air of gradually changing refractive index (denser near the ground, rarer higher up), and this bends light in interesting ways.
You may have seen objects wavering or flickering when viewed through hot air rising above a fire or a heater. The hot air just above the flame is lighter (less dense) and has a slightly smaller refractive index than the cooler air above it. Because these physical conditions keep changing, the apparent position of the object seen through the hot air keeps fluctuating. This is atmospheric refraction on a small, local scale. The twinkling of stars is the same effect on a much larger scale.
Twinkling of Stars
The twinkling of a star is due to atmospheric refraction of starlight.

Starlight, on entering the earth's atmosphere, undergoes continuous refraction through layers of gradually changing refractive index before it reaches us. Since the atmosphere bends the starlight towards the normal, the apparent position of a star is slightly higher than its actual position when viewed near the horizon.
Because the physical conditions of the atmosphere keep changing, this apparent position keeps varying slightly. Stars are so far away that they act as point sources of light. As the path of the light keeps changing, the amount of starlight entering our eye flickers - the star appears brighter at one moment and fainter the next. This flickering is the twinkling of the star.
Why Planets Do Not Twinkle; Advance Sunrise and Delayed Sunset
Why planets do not twinkle: Planets are much closer to the earth than stars and appear as extended sources (not points). A planet can be thought of as a collection of a large number of point sources. The variations in light from all these points average out to zero, so the twinkling effect is nullified and planets shine steadily.
Advance sunrise and delayed sunset:

Because of atmospheric refraction, the Sun is visible about 2 minutes before the actual sunrise and about 2 minutes after the actual sunset. "Actual sunrise" means the actual crossing of the horizon by the Sun. The atmosphere bends the light from the Sun (which is below the horizon) so that its apparent position is slightly above the horizon. The flattening of the Sun's disc at sunrise and sunset is also due to this refraction.
[Exam Tip] Stars twinkle (point sources); planets do not (extended sources). Atmospheric refraction gives us about 2 extra minutes of daylight at both sunrise and sunset.
Solved Examples
Example 1: Why stars twinkle
Explain, in terms of atmospheric refraction, why stars appear to twinkle.
Solution: Starlight passes through atmospheric layers of continuously changing refractive index and is refracted repeatedly. Since a star is a distant point source, small changes in the atmosphere cause the amount of light reaching the eye to keep fluctuating - the star appears alternately brighter and fainter. This fluctuation is seen as twinkling.
Example 2: Why planets don't twinkle
Why do planets not twinkle although stars do?
Solution: Planets are close to the earth and act as extended sources (many point sources together). The brightening and dimming from the individual points average out, so the net light reaching the eye stays nearly constant - the planet does not twinkle.
Example 3: Two extra minutes of daylight
Why is the Sun seen about 2 minutes before actual sunrise?
Solution: Light from the Sun, which is still just below the horizon, is bent by atmospheric refraction so that the Sun's apparent position is raised above the horizon. Thus we see the Sun about 2 minutes before it actually crosses the horizon, and similarly about 2 minutes after actual sunset.
Example 4: Apparent star position
Is the apparent position of a star near the horizon higher or lower than its true position? Why?
Solution: It appears higher than its true position. The atmosphere (denser near the ground) bends the starlight towards the normal, raising the apparent position of the star above its actual position when viewed near the horizon.