The Earth and the Moon are essentially the same distance from the sun, yet the two rocks have very different surfaces thanks to our Earth’s atmosphere. It shields us during the day, and at night, it traps enough heat to keep the planet’s surface from freezing. What exactly is going on with the gas molecules in our atmosphere? This explainer from Minute Earth and KurzGesagt illustrates how greenhouse gases actually work.
Plus, further reading on Greenhouse Gas at Wikipedia and The University Corporation for Atmospheric Research. An introduction:
The Goldilocks Principle can be summed up neatly as “Venus is too hot, Mars is too cold, and Earth is just right.” The fact that Earth has an average surface temperature comfortably between the boiling point and freezing point of water, and thus is suitable for our sort of life, cannot be explained by simply suggesting that our planet orbits at just the right distance from the sun to absorb just the right amount of solar radiation. Our moderate temperatures are also the result of having just the right kind of atmosphere. A Venus-type atmosphere would produce hellish, Venus-like conditions on our planet; a Mars atmosphere would leave us shivering in a Martian-type deep freeze.
Instead, parts of our atmosphere act as an insulating blanket of just the right thickness, trapping sufficient solar energy to keep the global average temperature in a pleasant range. The Martian blanket is too thin, and the Venusian blanket is way too thick! The ‘blanket’ here is a collection of atmospheric gases called ‘greenhouse gases’ based on the idea that the gases also ‘trap’ heat like the glass walls of a greenhouse do.
These gases, mainly water vapor (H2O), carbon dioxide (CO2), methane (CH4), and nitrous oxide (N2O), all act as effective global insulators.