The Greenhouse Effect
Earth is protected from the harsh cold of space by our atmosphere. The gases in our atmosphere change the way the energy from the sun reaches earth, and also the way energy from earth is lost again back into space. On places like the moon, which has no atmosphere, the surface warms very quickly in sunlight and cools very quickly in the dark, and the swings in temperature are extreme.
The atmosphere slows the warming and cooling of the surface of the earth and makes the swings in temperature tolerable for living things like us.
If there is a balance between the energy reaching the earth and the energy leaving the earth, then earth's overall climate is stable. (Of course, there is local variation due to distance from the equator and seasonal fluctuation.)