We know that every object on the surface of the Earth is attracted by the Earth.So,if an object has a mass'm',it will experience a force that is called the weight of the object which acts vertically downwards.
To move this object upward from 'a' to'b' through a distance 'h', we must apply a force 'f' upward were F=Mg
W=Fh, but as F=Mg,
W=mgh -------(1)
This work done on the object which changes its position is called the Gravitational Potential Energy. Then:
Ep=w-------(2)
From (1) & (2), we get:
Ep=mgh