Well, the guidance computer in the spacecraft calls VESSEL::GetGlobalPos() and VESSEL::GetGlobalVel()...
The real answer is lots and lots of help from the ground, and lots of radar and radio data. That and a mathematical algorithm called the Kalman filter. This is a magical little program that given your old course (position and velocity), the physics of gravity, and some measurements (such as radar distance to the spacecraft) calculates the most likely value for your current course.
Most of the time the orbit is calculated on the ground and sent up to the spacecraft -- not because the spacecraft computer isn't good enough, but because it is easier to calculate the answer on the ground and send it up than to send all the raw measurements from the ground to the spacecraft and have it figure it out.
Some maneuvers are calculated on-board, such as the Space Shuttle (and I imagine other spacecraft, manned and automatic) docking with the Space Station, and things like a lunar landing. In these cases the computer again starts with a course, usually sent up from the ground, then uses physics and its own instruments, such as star trackers, gyroscopes, accelerometers, and radars, to generate measurements. No one measurement is good enough to get the current course, but combined with the magic of the Kalman filter, the spacecraft is able to keep track of its course.
So, let's take an example like the Apollo lunar landing. Before Powered Descent Initiation (PDI) when the spacecraft is still in lunar orbit, the spacecraft is tracked by its radio signal. The ground sends up a signal which among other things contains a pseudo-random noise signal. Think of it as a very accurate timing signal. When the spacecraft receives this signal, it immediately copies it into its own downlink, so that when the signal is again received on the ground, the ground equipment knows exactly how long the signal traveled from the Earth to the spacecraft and back. This becomes a measurement. By combining a bunch of these measurements, using computers on the ground running the Kalman filter program among other things, the ground guys are able to put together the "track" of the spacecraft, which is why this process is called "tracking".
So, with this track, the ground guys are able to calculate a "state vector", which is the position and velocity of the spacecraft relative to the moon at a particular instant. By using a gravity model, this state vector can be "propagated" forward or backward in time. The spacecraft computer is easily able to handle this task. Of course since the measurement is made tools made by humans, it is imperfect and will become more imperfect, so they have to keep tracking the spacecraft and sending up state vector updates.
So again with a lunar landing, the spacecraft uses the uplinked state vector to figure out where it is. Eventually it will calculate that it is at the correct place and time to fire its engines and start towards the surface. Now that the engines are firing, the accelerometers in the spacecraft detect this, and this becomes a measurement which once again feeds a Kalman filter program running on its own computer. Since the spacecraft is not just coasting anymore, it needs to use a physics model which includes both gravity and thrust. As the spacecraft falls out of orbit, the vehicle is still tracked by the ground, but it also tracks itself. When the spacecraft gets close enough to the ground, it uses a radar to measure its altitude, but perhaps more importantly its speed both vertically and horizontally. These measurements are also fed into the filter.
So, a lander makes its way to the surface by a combination of lots of help from the ground, measurements from its own sensors, and the Kalman filter. Generally when a vehicle is far from anything floating in deep space, it will use the most help from the ground. When it is maneuvering or near some object like a space station or the surface, it will use its own sensors.
The space shuttle uses state vectors from the ground to execute a deorbit burn, but from there acts much like a lunar lander, using its own accelerometers to feel lift and drag through the atmosphere. Since it is landing on the Earth, it is also able to use GPS, and since it is landing in the United States, it is also able to use the same military navigation beacons on the ground that the Air Force uses. There are lots of different kinds of data, and the physics of flying are different, but the Kalman filter still supports it all.
None of this is secret, just complicated. I could explain further, but the next step requires linear algebra and matrix equations.