When we move a single charge q through a potential difference DV, its potential energy changes by qDV.
Charging a capacitor involves moving a large number of charges from one capacitor plate to another. If DV is the final potential difference on the capacitor, and Q is the magnitude of the charge on each plate, the energy stored in the capacitor is:
U = 1/2 QDV.
The factor of 1/2 is because, on average, the charges were moved through a potential difference of 1/2 DV.
Using Q = CDV, the energy stored in a capacitor can be written as:
U = 1/2 QDV = 1/2 C(DV)2 = 1/2 Q2/C