Say we have some initial voltage difference, $\Delta V$, between a battery and a capacitor.
This means that first electron arriving from the negative terminal of the battery to the negative terminal of the capacitor and another electron electron simultaneously arriving from the positive plate of the capacitor to the positive terminal of the battery have to lose the energy corresponding to that voltage difference, i.e., $e\Delta V$.
As the voltage on the capacitor increases, the voltage difference decreases and, therefore the losses per unit charge decrease as well, averaging at $e\Delta V/2$.
If, as you are suggesting the wires had no resistance, the energy would be burned on the internal resistance of the battery.
If the battery had no internal resistance, then the electrons would speed up in the wires and have an abrupt stop at their destinations with the excessive kinetic energy converted to heat anyway.
We can potentially avoid these losses, if we insert an inductor in series with the capacitor.
In this case, the initial current will be zero and the current will be ramping up gradually. When the voltage on the capacitor equalizes the voltage on the battery, the current will reach its peak and will start declining, while still charging the capacitor. When the voltage on the capacitor reaches 2x of the battery voltage, the current drops back to zero. If, at that very moment, we break the circuit, we'll end up with the charged capacitor and no losses (except minor losses in the wires).