sunlight (sunlight is photo energy) warms the Earth, and molecules in the atmosphere intercept the resulting heat before it can escape to space.
What indeed?
It is a symbiotic relationship between light energy and the gaseous content of Earth's atmospheres. Earth is also within an orbital location called "The Habital Zone." It is sort of like the choices of Baby Bear of Goldilocks' Three Bears; "Not too hot and not too cold."
Incoming ultraviolet, (click here) visible, and a limited portion of infrared energy (together sometimes called "shortwave radiation") from the Sun drive the Earth's climate system. Some of this incoming radiation is reflected off clouds, some is absorbed by the atmosphere, and some passes through to the Earth's surface. Larger aerosol particles in the atmosphere interact with and absorb some of the radiation, causing the atmosphere to warm. The heat generated by this absorption is emitted as longwave infrared radiation, some of which radiates out into space.