The is done by compressing and expanding the refrigerant, which causes the refrigerant to change state and in the process, to become either hotter or cooler. Energy is absorbed from the heat source at the cold part of the cycle, upgraded by the compressor and then the heat can be removed from the refrigerant cycle for use in the home. The electrical input to the heat pump powers the compressor in order to increase the pressure of the refrigerant and power this cycle.
The compressor turns warm refrigerant gas into high pressure hot vapour which passes to the heat exchanger (condenser side) for the heat to be extracted for use in the home. This heat exchanger (condenser) condenses the hot pressurised vapour into a high pressure medium temperature liquid. This liquid then passes through an expansion valve which allows the pressure of the fluid to drop rapidly. The drop-in pressure causes the fluid to cool, allowing it to pass into the second heat exchanger (Evaporator). This is where the heat pump absorbs heat from the source. The low temperature and pressure refrigerant fluid has a very low boiling point and so boils in this heat exchanger absorbing energy from the source in the process. Finally the warm refrigerant gas passes back to the compressor and the cycle starts over again.
A heat pumps efficiency depends on how effectively this heat can be moved from the heat source to where it is required. As the temperature difference between the two heat exchangers increases, the required pressure difference between the exchangers to make the refrigerant boil and condense also increases. This higher pressure difference requirement in turn, means to make the cycle work the power required at the compressor is greater. This is why the efficiency of a heat pump improves as the temperature difference between the source and the target reduces
This is why Air source heat pumps can be less efficient than ground source in the winter. This is also why a good installer will always try to make sure that when using any heat pump, your heating distribution system is sized to allow the minimum flow temperature possible. This is also why various methods of increasing source temperatures, such as solar assisted heat pumps, can offer efficiency benefits to the running of a system.