The phenomenon this question relates to is the photoelectric effect, and it can only be explained using Einstein’s photon model of light.
Electromagnetic waves are emitted in discrete lumps of energy, called photons.
The energy carried by one of these photons is given by E = hf = hc/l where l is the wavelength of the wave ( f=c/l ) , and h is the planks constant.
If a photon collides with a free electron in the surface of the metal, then the electron absorbs all of the energy of the photon. So the electron gains energy equal to hf.
However, energy is required to remove this electron from the metal. This is due to the negative charge of the electron and the positive charge of the nuclei in the metal atoms causing an attraction between them.
The energy required to remove the electron is the 'work function energy', p.
So the maximum kinetic energy that an electron can have once it has absorbed a photon and left the metal is KE = hf – p.
This is because the electron initially gains ‘hf’ of kinetic energy from the photon but then loses ‘p’ energy as it does work against the forces holding it in the metal.