To keep the food in the fridge cool, we make the air inside cold. But even if we did that and kept the door closed all the time, the air inside would gradually warm up. The food in the freezer is lost of the power is off for a long period. So when the air inside starts to become warm, the motor comes on and heat is pumped out until it is cold enough again.

The cold air inside gradually warms up because heat is always trying to flow from
the warm air outside (in the room) into the cold chamber.
However good the insulation, some heat will creep in - plenty
will flow in when the door is opened
Hold your mouse over the image to activate the sequence. This
begins with the visualization of heat leaking through the
insulation, and raising the temperature of the air inside. When
the air temperature rises, the unit switches on and starts to pump
heat out of the cold chamber and back into the room. Meanwhile,
of course, heat is still leaking in. But the unit quickly pumps
enough heat out to bring the inside air temperature down again,
and then it switches off. This is a very simple thermostat control
system which tells the unit to run if the cold air temperature is too high,
and tells it to stop once the correct temperature is reached.
To
understand how the heat is pumped, or made to flow out of the
cold chamber and back into the room, take a look at Understanding
Refrigeration.

The principle of the heat pump is just the same as for the fridge, except that now we are interested in the heat which is delivered. For the fridge this heat was just discarded back into the room, but now we are actively trying to warm up the building. The heat is taken from the air outside and pumped into the building.
Hold your mouse over the image to activate the sequence. This begins with the visualization of heat leaking through the walls of the building, and reducing the temperature of the air inside. Of course the air outside is warmed up a bit, but no-one notices that - we normally just turn up the heating and forget that all the heat eventually goes outside and heats up the whole of Berkshire or wherever. Now, when the rooms start to cool down the heat pump can be turned on to pump heat back inside.
A building is much larger than a refrigerator, and its best to
make the heat pump as small as possible, so that it costs less.
The heat pump would be ON almost continuously in cold weather, delivering
heat at the same rate as it leaks away through walls and ceilings,
open doors and windows, and through the floor.
To
understand how the heat is pumped, or made to flow out of the
cold region to a warm region, take a look at Understanding
Refrigeration