Sorry, I was addressing the statement that “It’s not like they had optical sensors monitoring the position of the Sun, Moon, and Earth,”; while the Apollo system didn’t, later ballistic missile systems and interplanetary probes do, in fact, use optical stellar measurements for alignment. How they do this is fairly simple in concept, and essentially identical to how sailors navigated before LORAN and GPS. That is to say, they took a reference on some “fixed” celestial light source (like the Sun at local noon or a co-axial star like Polaris) and measured the angle from the horizon, then referred a reference table to find the latitude. Similarly, astronauts will take two or more sets of sightings between fixed points and triangulate (or look in a cross-referenced table) to determine their position and orientation. Velocity is determined by measuring the difference in position between sightings. For a vessel in a conic section orbit, you have to take multiple sightings in order to figure out the shape of the orbit and location within it.
Once you have position and velocity, you just punch that into the navigation computer, and it figures out the impulse curves (i.e. when and in what direction to apply thrust) for a desired (usually pre-programmed) final velocity and position; in the targets world this desired objective is called a “pierce point”; in orbital mechanics, this is usually called a trans-whatever injection orbit or final orbit. Although in theory this can be done manually on a manned vessel (as with the multiple correction burns during the Apollo XIII abort) in normal operations the engine firing is performed by an automatic system for both precision and because it is very easy to become disoriented during such maneuvers.
The Earth’s magnetic field is never used as a reference for rockets and missiles because it is just too inconsistent to be useful. Even aircraft pilots frequently have problem when flying over mountains or areas with large iron deposits. Some early work was done with cruise missiles to try to use magnetic guidance but it proved to be too unreliable, and most systems used inertial measurement prior to the development of terrain contour matching (TERCOM) systems.