Let us see some thermodynamics of a heat pump. The concept sounds impossible, and I hope this helps you understand why it's possible.
Refrigerators and heat pumps are both devices that push heat against the temperature gradient, much like a truck pushes a load against the gravity gradient when climbing up a hill. Spontaneously heat flows from hot to cool only, that is, with the temperature gradient, never the against it. If you want to push against the gradient, you need to do work.
It is initially difficult to understand how a heat pump can bring heat from the ground at +4 C to the room at +21 C, because intuition says that it's impossible. I have thought out a way to understand it intuitionally. Imagine a house with a heat pump. Now, because the house leaks some heat anyway, all heat put in will eventually end up outside - so, just take the house away. What we have left is that the pump compresses heat, which is decompressed again. There we have an equilibrium - no heat in, no heat out.
Imagine adiabatically compressing air into a bicycle pump, and then releasing it. The fact that the air gets hotter first doesn't mean that the heat appears from thin air. The energy is merely compressed in a smaller volume for a moment in an adiabatic compression. Then you release it - everything is like before. There you can see that the heat pump doesn't do any magic, it merely compresses the heat into the house, which eventually decompresses it by leaking it out. The people inside experience an apparent gain of heat energy, but they're forgetting that all the heat eventually ends up where it came from.
A heat pump is thermodynamically more efficient than, say, electric heating, which converts 1 kWh of electricity to 1 kWh of heat. The reason is simple. All the work done by the heat pump ends up as frictional heat, but in the process the pump has also brought heat from the outside. The purpose of the pump is to supply heat, so even friction is then purposeful work.
NOT EXACTLY. After a course in chemical engineering, that is, some real flow resistance calculations, I could see that the losses from a heat exchanging grid (as J/kg of fluid) are far from insignificant. There is also a more important loss. The water that exits from the house is at room temperature 20 C, not at ground temperature 4 C. It is impossible to make the radiators lower the temperature more than to 20 C. The water is completely cooled in the ground, giving off a lot of heat. This is compensated by the pump's efficiency, but it is definitely important.
An example: the pump has the coefficient of performance of three, which is around the actual value. For one kWh of electricity, the pump gives three kWh of heat. However, the machine also heats up because of friction, so it gives out one kWh of heat, which is exactly the same energy that was put in. So, we have four kWh of heat for one kWh of electricity, which is four times more efficient than electrical heating.
It is clearer when written down quantitatively. Let us set
ρ = coefficient of performance
QC = heat supplied
W = work done by the machine.
δQtotal = the total heat gain
Wρ = heat supplied by the machine
W = energy put in to the machine
By definition of the coefficient of performance,
ρ = QC / W
Thus,
δQtotal = Wρ + W = W(ρ + 1)
Energy put into the machine (W) must equal the energy coming out from the machine (W), if the machine isn't heating up, eventually melting, vaporising and exploding. Energy must be conserved.
The most vigilant might notice the little and insignificant major inefficiency there is in a heat pump. The fluid flowing in the pipes outside meets some flow resistance, which causes frictional heat. Part of this heat Essentially all leaks to the ground outside.