How do greenhouse gases trap heat?
A greenhouse gas, like carbon dioxide or methane, allows visible light and shorter wave radiation to pass through it. In other words it is transparent to that type of energy. Much like how the windows of a greenhouse allow sunlight to enter into the greenhouse (see the greenhouse effect).
When that shortwave energy hits the Earth it heats the Earth. The Earth then emits heats or infrared energy which is what we refer to as long wave radiation. The heat does not pass through greenhouse gases as the gases are not transparent to radiation of that wavelength. Much the same way that the windows of a greenhouse trap the heat inside the house. So the greenhouse gases keep the Earth warm by preventing the heat from the ground from escaping.
Greenhouse gases keep the Earth warm by preventing heat from escaping, they do nothing to actually warm the Earth.
The greenhouse effect is when sunlight enters the atmosphere, hits the Earth and is changed to heat and then that heat is radiated is back to space. Certain gases in the atmosphere block that heat from escaping. Then it is all remitted back to earth in all direction therefore forcing climate change.
It all has to do with the wavelength.
Electromagnetic radiation travels in waves, and the distance from one wave crest to the next is known as the wave length.
Generally speaking when these wave encounter something they will either through it or be blocked by it. A general rule of thumb is the smaller the wavelength the more likely it will pass through the material.
In the illustration above you can see the smaller wavelengths are things like x rays and gamma rays. If you have had to have an x-ray you know that x-rays travel freely through a lot of material (such as your skin). The calcium of your bones will block some x-rays (which is why they appear on the xray film), but the reason that you wear lead everywhere that you do not need x-rayed is that it takes lead to fully stop x-rays. Gamma rays are even harder to block, and need a much thicker lead shield.
What does this have to do with greenhouse gases?
Well you see on the picture above the area that is a rainbow? That is where most of the radiation from the sun falls, and we call it visible light. Energy at this wavelength passes freely through the atmosphere, and can strike the Earth. When it strikes the Earth the energy is absorbed and the Earth is heated. Heat is actually another form of electromagnetic radiation called infrared radiation. What is crucial to note is that this type of radiation has a longer wavelength.
Greenhouse gases (carbon dioxide, methane, water vapor, etc.) are transparent to visible light, but opaque to infrared radiation. That means the sunlight travels through them but the heat is blocked by them. That is how greenhouse gases trap heat.
Side note, we call heat infrared radiation because it is adjacent to the red end of the visible light spectrum. We call the radiation adjacent to the other side of the visible light spectrum Ultraviolet radiation, because it is adjacent the the violet end of the visible light.
Additional side note, the sun actually emits radiation over a wider spectrum than just the visible light spectrum.
The Ultraviolet part of the solar energy is blocked by ozone in the upper atmosphere (which is why we need the ozone layer). The infrared radiation of the inbound solar radiation is also blocked from entering by greenhouse gases.