This conceptual question is asked frequently on this site—search spring energy half
here for many other answers. The confusion generally arises because when dealing with the ideal spring and a constant load, half the applied work seems to magically disappear into heat (strictly, thermal energy). We see below that this is the only reasonable conclusion for our models and idealizations to mesh; nevertheless, one might ask: How does the spring (or capacitor, in the twin example of an idealized circuit with a constant voltage) know to turn half the work into heat? It's a fun puzzle that can be explored in the undergrad curriculum of mechanical engineering, electrical engineering, or physics, for instance.
Let's build up the evidence leading to this conclusion:
We can certainly apply a constant force on both ideal and real springs (for simplicity, by "real springs" I mean springs with mass and friction that still deflect linearly); consider hanging a weight on the end, for example. In addition, modeling expediency requires the existence of ideal forces not associated with any physical mass that needs to be accelerated.
The strain energy stored in both ideal and real springs is $\boldsymbol{\frac{kx^2}{2}}$. We obtain this by stretching the spring quasistatically and reversibly, applying varying force $F=kx$ from zero deflection to the final deflection $d$. This approach maintains constantly balanced forces and thus avoids energy dispersion to the environment. The integration step is shown in @JohnHunter's answer. (This is the only time a varying force is used in this answer.)
The ideal spring has no mass and thus no inertia. It cannot accelerate to produce gradual motion. For an applied force, all responses occur instantaneously, producing immediate and final deflection $d=F/k$. (In contrast, real springs have mass and internal friction; when a constant force is applied, the real spring accelerates and exhibits damped oscillation to ultimately obtain its equilibrium final position, making the frictional losses obvious.)
A constant force applied over deflection $x$ corresponds to work $Fx$. We know from above that half of this work goes into strain energy. What about the other half? For a real spring, we can apply the laws of motion and obtain the solution for damped motion, concluding that the other half goes into some time-varying combination of kinetic energy and thermal energy. (If the spring is critically damped or overdamped, then no kinetic energy remains at $d$.)
Finally, how should we treat the ideal spring, for which no motion over time can occur? The only reasonable reconciliation is that in addition to completing instantaneous deflection $d$ (instantaneously storing strain energy $\frac{kx^2}{2}$), we also complete instantaneous dissipation $\boldsymbol{\frac{kx^2}{2}}$ into thermal energy.
In other words, idealizing the ideal spring as massless and frictionless, in addition to assuming the existence of incorporeal ideal forces, implies that certain dissipative dynamics occur instantaneously, leaving only their outcome; it's all part of the ideal spring model "package."
In the end, the automatic division into halves arises from the assumptions of linear elasticity, the definition of work, and the law of conservation of energy.