Although it is impossible for heat energy to spontaneously run from a cold place to a hot one, it is possible to pump heat energy from cold to hot. This is just the thermodynamic equivalent of pumping water uphill. The technical details of how one pumps heat "uphill" are a bit more involved than those for water, however, so let's examine refrigeration in detail.
The diagram at left illustrates the basic thermodynamic steps used in commercial refrigerators, freezers, and air conditioners. We start with a sealed circuit of tubing which contains a gas. In principle this can be any gas -- in practice, if you want your cooling cycle to be even minimally energy-efficient, you need to select a gas that has a high heat capacity and which liquifies easily. (For decades the gas of choice was freon, CCl2F2, until it was realized that freon in the atmosphere degrades the ozone layer. Patented gases with complex chemical formulas are used now.)
The cooling cycle starts with the pump (bottom right), which is the device that makes all the noise and draws all the electric current on refrigerators and air conditioners. The pump rapidly compresses the gas. This not only heats the gas up (analogous to the flaming tissue paper in the test tube experiment), but if we are using an easily-compressible gas, also condenses it into a liquid! This warm liquid is then sent through a long section of tubing exposed to the air so that the liquid can cool down. You can see these cooling coils on the back of any refrigerator; for air-conditioning units they are located in the part of the unit that is outside the house.
After the warm liquid has been cooled more-or-less to room temperature (top right of diagram), it backs up behind a pressure valve which releases the liquid into a partial vacuum (blue section of tubing). The liquid instantly boils, exactly the same way that liquid nitrogen boils when it is at room temperature. The boiling process absorbs lots of heat energy, in much the same way that sweat evaporating from your body does, and as the boiled-off gas expands into the partial vacuum, it cools down even further. This very cold gas is then sent through tubing that surrounds the interior of the refrigerator (or through coils that have a fan blowing across them, in the case of an air conditioner). Your food gets cold. After going through the inner coils, the gas is now back at room temperature (more or less), and is ready to be compressed and cycled again by the pump.
In terms of energy, all we have done is extract heat energy from the interior of the fridge (or house), then blown it out the back. The heat exhausted exactly equals the heat extracted, so conservation of energy is obeyed. This is not the problem. The problem is, the heat doesn't want to flow from your cool living room to the hot outdoors, and it wouldn't -- except that you are burning electricity to pump there. On more than one occasion, back when I was in college, I saw guys in my (non-air-conditioned) dorm trying to cool their room by leaving the door on a refrigerator open. Well, since the heat-radiator coils on a fridge are located on the fridge, this means that all the guys were doing was pumping heat from the front of the fridge to the back -- while also running an electric refrigerator motor in their room at about 200 watts. The guys would have been better off leaving their refrigerators closed and turning on a 200-watt space heater. The room temperature would have been the same, and at least they could have had a cold beer.