Stars twinkle because of atmospheric refraction. As starlight passes through the Earth's atmosphere, which is composed of layers with varying temperatures and densities, it is continuously refracted. This refraction causes the apparent position and brightness of the star to fluctuate rapidly, leading to the twinkling effect. Planets do not twinkle because they are much closer and appear as extended sources of light rather than point sources, so the atmospheric effect is averaged out.