Heat pumps work the same way a refrigerator does--by having a gas condense to liquid and then expand again, within a loop of rigid tubing that extends from the hot side to the cold side. This moves heat because of the ideal gas law, which says that as pressure increases, temperature increases; as pressure decreases, temperature decreases.
Thus, when the gas expands on one side of the tubing loop, its temperature decreases and it pulls heat into it from the surroundings. This becomes the cold side of the loop. The gas is then pumped to the other side of the loop and pressurized by an electric compressor until it becomes a liquid. This rise in pressure raises the temperature, so the liquid pushes heat out to its surroundings. This becomes the hot side of the loop. After dumping its heat outside, the fluid goes through a valve to allow it to expand to a gas again on the other side of the loop, starting the cycle over.
Diagram of a heat pump cycle, in cooling mode
Diagram of a heat pump cycle, in heating mode
In theory, one side of the tubing loop is indoors while the other is outdoors, but in reality this is usually costly and difficult to plumb, because the loop's "working fluid" is a volatile gas. These gases should not be allowed to escape, because they usually have adverse environmental impacts such as climate change and ozone depletion. Thus, usually both sides of the loop are in the heat pump unit, and the "indoor" side contacts indoor air brought to it by a duct, or contacts a separate loop of water that circulates to ducts or radiant systems.