Photon has no rest mass but when photon travels at the speed of light, it gains momentum.
The energy of the photon is E= pc, c = speed of the photon and p= its momentum.
∴ E = pc
but p = h/λ (This comes from De-broglie's theory)
∴ E = hc/λ
frequency = ν = c/λ
∴E = hν