what about the electrostatic potential energy which happened when the electron got separated from the atom?
That's what the work function is.
If you consider a single atom then to eject an electron you have to supply an energy equal to the energy of the highest energy electron. For example in a hydrogen atom the highest electron energy is $-13.6$ eV so you have to supply an energy of $13.6$ eV to eject the electron. So if you use a photon to ionise the hydrogen atom the kinetic energy of the ejected electron will be:
$$ T = h\nu - 13.6~\textrm{eV} $$
In solids the atomic energy levels merge together to form bands. Electrons in a band are in effect bound to the whole array of the atoms in the crystal rather to a single atom. Within a band we still have a highest energy electron, and this energy is called the Fermi level. To eject an electron from the solid you have to supply an energy equal to the Fermi level, and this energy is what we call the work function (denoted by $\phi$). So the kinetic energy of the ejected electron is:
$$ T = h\nu - \phi $$