The Sun warms the Earth, and the warm surface radiates heat back towards space as infrared radiation. Some gases in the atmosphere absorb part of this heat. These are called greenhouse gases, and they include carbon dioxide, methane and water vapour.
After absorbing the heat, the gases release the energy again in all directions. Some of this energy travels back towards the Earth's surface and warms it. Less heat escapes into space than if the gases were not there.
This process keeps the Earth warm and is called the greenhouse effect. Without it the Earth would be much colder, and it would be very hard for life to survive.