The way that greenhouse gases work does not involve transferring heat from a cold body to a hot one, and there is no violation of the laws of thermodynamics. You should think of greenhouse gases as a blanket around the Earth. Just as a blanket keeps you warm by trapping your body's own heat and not letting it escape, greenhouse gases trap the heat that is radiated by the Earth and don't let it escape into space.
Roughly the way it works is that the atmosphere is transparent to the radiation coming from the sun, so that radiation falls on the surface of the Earth and heats it up. The warm Earth then re-radiates some of that energy, but since the Earth is cooler than the sun, the wavelengths radiated by the Earth are primarily in the infrared region, larger than those from the sun which are primarily in the visible region. The longer wavelengths are trapped by the greenhouse gases in the atmosphere.
Incidentally, you can see pictures of the Earth taken with an infrared camera in Episode 6 of the "Science Off the Sphere" feature on PhysicsCentral. The brightest spots are the hottest, where the most radiation is coming from. Some of that radiation is trapped by the atmosphere, but, as you can see, a lot of it still gets into space where the camera can detect it.
Alan Chodos, PhD
Associate Executive Officer
American Physical Society
Gav from Australia