The HUD is an outgrowth of the reflector or optical gunsight developed at the end of WWI and which started to appear on most fighters by the mid 30s. The secret to them is the collimator, or collimating lens, which is the big magnifying glass looking thing on a reflector sight.
A collimator aligns the light rays of an object to be parallel after they pass through the lens. The light rays being parallel, if you look at a object through the lens, it will be focused at infinity (it will look like it's a few hundred feet in front) even though it's only inches away. If you shine a light through a plate with a circular slot and hole in the middle, project the light off a mirror straight up, through the collimator, then reflect it back using a sloping sheet of glass (or the windshield), and you the pilot get in line with the projected light's rays, you will see an illuminated circle and dot (called a reticle) that looks like it's suspended in space several hundred feet in front.
You can only see the reticle if you are where the light rays are being projected, and there is almost no parallax error, so as long as you have the reticle in view, you are correctly lined up with a target up front. If you move your head to the side or up or down, the image will start to disappear off the side; but there is a fair amount of leeway, and your eye alignment doesn't have to be perfect, just enough to keep the reticle in view. This was so much easier than keeping your eye perfectly aligned with a ring and bead sight.
The effect is similar to looking at something projected on a movie screen at a drive in, while your car windshield is painted black except for a little square you can look through. If your head's in the right spot, you can see the image on the screen out front, focused way out there, but if you move your head the image starts to disappear.
In the 60s instead of just projecting light through a plate with slots and cross hairs etc., they started to use CRTs and project more information, like radar indications, horizon indications and such. By the 70s they where sophisticated enough to produce all the symbology you see on modern HUDS.
But in the end, a HUD is just super fancy version a WW2 gunsight.