(N/A) capacitor stores energy in the form of an electrostatic field between its plates. When charging a capacitor,work is done by an external agent to move charge from one plate to the other against the existing potential difference.
Consider two uncharged conductors $1$ and $2$ as shown in the figure.
Imagine a process of transferring charge from conductor $2$ to conductor $1$ bit by bit. As charge is transferred,conductor $1$ becomes positively charged and conductor $2$ becomes negatively charged.
In transferring positive charge from conductor $2$ to conductor $1$,external work must be done because conductor $1$ is at a higher potential than conductor $2$.
Consider the situation when the conductors $1$ and $2$ have charges $Q'$ and $-Q'$ respectively.
The potential difference $V'$ between conductors $1$ and $2$ is $V' = \frac{Q'}{C}$,where $C$ is the capacitance of the system.
If a small charge $\delta Q'$ is transferred from conductor $2$ to $1$,the work done is $\delta W = V' \delta Q'$.
Substituting $V'$,we get $\delta W = \frac{Q' \delta Q'}{C}$.
The total work done $W$ to bring a total charge $Q$ from conductor $2$ to $1$ is obtained by integrating the work done for small charge transfers from $0$ to $Q$:
$W = \int_{0}^{Q} \frac{Q'}{C} dQ' = \frac{1}{C} \left[ \frac{(Q')^2}{2} \right]_{0}^{Q} = \frac{Q^2}{2C}$.
Since $Q = CV$,the energy stored can also be expressed as $U = \frac{1}{2}CV^2$ or $U = \frac{1}{2}QV$.