It traps only earth's radiated heat due to the selective wavelength region it absorbs.
The sun radiates heat at a very high temperature (6000K) and in the form of shortwaves (having short wavelength). The greenhouse gases allow these gases to enter the earth's atmosphere as they are of less wavelength (0.2-3 μm). Now, the energy that is radiated back from earth is at a very less temperature (300K) and in the form of longwaves infra-red waves (having high wavelength). The greenhouse gases do not allow these waves of high wavelengths (5-30 μm) to leave the atmosphere and 'trap' them.
~7 μm region is absorbed by water vapour, ~15 μm is absorbed by CO2 which are formed only by the earth's radiation and not sun's radiation, that is why it traps only earth's heat.