The atmosphere is made up of several layers. The upper layer is optically rarer, but the bottom layer is optically denser. Refraction occurs as a result of this when light passes through different layers of the atmosphere. As light travels through the atmosphere, it bends towards the normal as it passes through thicker and denser layers.
The stars are present very far away from us. The light coming from them get refracted multiple times and that results in the twinkling of the stars. However, planets are located near to us, and there is less refraction of light. Therefore, stars twinkle while planets do not.