First of all, a heat pump does not generate heat. It transfers heat from a source at lower temperature to a user centre at higher temperature. All heat pumps are driven by electricity.
Sources of heat can either be air, water or ground. Heat pumps work by refrigerant circulating in a closed loop, under the action of a compressor. This moves refrigerant gas to the condenser, where it is changed into its liquid phase. In doing so, it gives up its latent heat of evaporation, which is the useful energy contributed via the heat exchanger.
Liquid refrigerant then passes through the expansion valve, where system pressure is reduced. On reaching the evaporator, flow can then change back into its gaseous phase. To do so, it must absorb latent heat of evaporation, and this it gets via a heat exchanger from source: either air, water of ground. Thus, in the course of two phase changes, heat is transferred from lower to higher temperature.