Description→
When a capacitor is charged, the work is done by charged battery (i.e the chemical energy of the battery is used to charge the capacitor). As the capacitor gets charged, the potential difference between its plates increases. Due to this increase in the potential difference between the plates, the battery has to give the same amount of charge to the capacitor. Because of that, the battery has to do more and more work.
The total amount of work done in charging the capacitor is stored in the form of electric potential energy in between the capacitor plates. This energy is retrieved as heat when the capacitor is discharged through a resistance.
Derivation→
Let us consider, a capacitor of capacitance $C$with a potential difference of $V$ between the plates.
In the process of charging, electrons are transferred from the positive to negative, unit each plate acquires an amount of charge $q$. Suppose during the process of charging, we increase the charge from $q'$ to $q'+dq'$ by transferring an amount of negative charge $dq'$ from the positive to the negative plate. the work done
$dW=V' \: dq'$
$dW=\frac{q'}{C} \: dq'$
Therefore, the total work done in charging the capacitor from the uncharged state (i.e. zero charge in the capacitor) to the final charge $q$ will be
$W=\int dW$
$W=\int_{0}^{q} \frac{q'}{C} \: dq'$
$W=\frac{1}{2}\left[ \frac{q'^{2}}{C} \right]^{q}_{0} $
$W=\frac{1}{2}\left[ \frac{q^{2}}{C} \right]$
$W=\frac{1}{2}\:C \: V^{2}$
This work done is stored in the form of potential energy $U$ within the capacitor. Thus
$U=\frac{1}{2}\:C \: V^{2} $
$U=\frac{1}{2}\frac{q^{2}}{C}$

Energy Stored in Combination of Capacitor→
If many capacitors are combined in series, or in parallel, the total potential energy stored in either combination is equal to the sum of the potential energies stored in the individual capacitor. This follows from the combination of the capacitor's expressions:
The potential energy is stored in a series combination→
The potential energy stored in a series combination (here $q$ is constant) is
$U=\frac{1}{2}\frac{q^{2}}{C}$
For series combination the capacitance of capacitor i.e. $\frac{1}{C}=\frac{1}{C_{1}}+\frac{1}{C_{2}}+\frac{1}{C_{2}}+..........$. Now substitute the value of $\frac{1}{C}$ in above equation
$U=\frac{1}{2}q^{2} \frac{1}{C_{1}}+\frac{1}{C_{2}}+\frac{1}{C_{2}}+.......... $
$U= \left( \frac{1}{2}q^{2} \frac{1}{C_{1}}+\frac{1}{2}q^{2} \frac{1}{C_{2}}+ \frac{1}{2}q^{2} \frac{1}{C_{2}}+.......... \right)$

$U=U_{1}+U_{2}+U_{3}+.........$

The potential energy is stored in a parallel combination→
The potential energy is stored in a parallel combination (here $V$ is constant) is
$U=\frac{1}{2}CV^{2}$
For parallel combination the capacitance of capacitor i.e. $C=C_{1}+C_{2}+C_{3}+......$. Now substitute the value of $C$ in the above equation
$U=\frac{1}{2} \left( C_{1}+C_{2}+C_{3}+........... \right) V^{2}$
$U= \frac{1}{2} C_{1} V^{2} + \frac{1}{2} C_{2} V^{2} +\frac{1}{2} C_{3}V^{2} +..... $

$U=U_{1}+U_{2}+U_{3}+......$
