Why is an $LC$ oscillator lossless, but $C V^2 / 2$ energy is lost to a capacitor connected to an ideal voltage source? It is mathematically proven that in an $LC$ oscillation that  all the energy gets transferred from the inductor to the capacitor and vice versa.
There is no energy loss as there is no load in the circuit.
However, in another case when a capacitor is directly connected to an ideal voltage source $C V^2 / 2$ of the energy gets dissipated via the capacitor.
What is the exact process involved in the latter case and why it does not apply to the first one?
 A: A battery connected to a capacitor is an RC circuit in the limit $R \to 0$ (i.e., there is no resistor and the resistance of the wire is negligible). One might think that the energy loss is zero in this limit, but this is not the case.
For an RC circuit with a battery and an initially (i.e., at $t=0$) uncharged capacitor, we have
\begin{equation}
Q(t) = CV (1 - e^{-t/RC})
\end{equation}
and
\begin{equation}
I(t) = \frac{V}{R} e^{-t/RC}.
\end{equation}
As $t\to\infty$, the energy loss due to the resistance is
\begin{equation}
E_{\mathrm{loss}} = R \int_{0}^{\infty} I(t)^{2} dt = \frac{1}{2} CV^{2}.
\end{equation}
This accounts for the difference between the work done by the battery and the energy stored in the capacitor. Notice that the above result does not depend on $R$. That is, one cannot ignore the energy loss even if $R$ is negligibly small.
What if the resistance is strictly zero, say, if we made a circuit entirely from superconductors? Then, we should not ignore the inductance of the circuit. It is an LC circuit with a very small $L$. In this case, the capacitor never reaches a fully charged state (i.e., $Q=CV$ and $I=0$). The charge and the current in the circuit oscillate forever as would in any ideal $LC$ circuit. The difference between the work done by the battery and the energy stored in the capacitor should be equal to the magnetic energy due to the inductance.
A: An ideal capacitor never "dissipates" energy, it merely stores it. The amount of energy stored in a capacitor is given by the formula you mentioned: $U = \frac{1}{2}CV^2$.
In the case of the LC circuit, the energy stored in the capacitor moves into the inductor in form of magnetic field energy and then goes back and forth from them.
In the case of an ideal battery, the capacitor will keep getting charged until the voltage across the plates is equal to the voltage of the battery. Since we have no voltage difference anymore, there will be no currents anymore. The capacitor stores electrical energy, but you can harness it for example by disconnecting the battery and connecting a lamp to the capacitor. The lamp will light up for a moment.
Now what does dissipate energy is any form of resistance, e.g. a lamp. Ideal capacitors and inductors don't.
