A body on Earth's surface can be positioned easily knowing the GPS coordinates, assuming the body is in direct path or 4 GPS satellites. But how is a satellite (say Maven or India's Mangalyaan) which is travelling to Mars positioned, or its position calculated to steer it on the right path? I guess the GPS satellites are facing the Earth not to waste power and resources by facing away from the Earth. But how is a satellite out of reach of GPS satellites positioned, and its position calculated with required precision?
Knowledge and maintenance of a spacecraft's state (its position, velocity, attitude, and attitude rate) is a collaboration between ground controllers and the spacecraft itself.
Vehicle guidance, navigation, and control systems
Some vehicles keep track of both translational state (where they are) and rotational state (where they are pointing). Others just keep track of rotational state; determining translational state is done by people back on Earth.
The software that does this is a Kalman filter. A Kalman filter maintains a notion of the spacecraft's state and the uncertainty in that state. The filter uses two basic operations to advance the state and uncertainty over time, propagate and update. The propagate step advances state and its uncertainty per a set of differential equations (e.g., F=ma). The update step refines the state per sensor readings that reflect on the state.
Some sensors such as accelerometers provide information used in the propagate step rather than in the update step. The position error grows without bound when position is propagated from sensed accelerations only. Another name for this is "dead reckoning". Measuring position is not easy. Solving the dead reckoning problem was the primary motivation for developing GPS.
Others sensors such as star trackers provide information that can be used in the update step. This means that attitude is far easier to keep track of in space than is position.
Another thing that makes propagating position difficult is that accelerometers only sense due to non-gravitational forces. They cannot sense acceleration due to gravity. This means that a spacecraft inertial navigation system that does estimate position (not all do) needs to model gravitation in the flight software. This is not easy for a vehicle in orbit about a rocky body. The Earth, the Moon, and Mars have non-spherical gravitational fields. In addition to having a model of the non-spherical nature of the gravity field, the flight software must also have a model of the rotation of the body about which they are orbiting.
While landers do have to know where they are to a high degree of precision, orbiting vehicles oftentimes don't. Many orbiters do not use their inertial navigation system to propagate position. Translational state is propagated only to the extent needed to perform the trajectory correction maneuvers commanded by the ground.
During the earliest missions to Mars, these correction burns were in the form of timed burns. The vehicle pointed itself in a commanded orientation and then fired its thrusters for a commanded duration with the burn starting at a commanded time. There was a big problem with the timed burn approach: They weren't very accurate. Having a five to ten percent variance in commanded versus actual $\Delta v$ was very common, even with a well-calibrated thruster.
Nowadays those maneuvers are instead commanded in terms of a commanded $\Delta v$ to be performed at a commanded burn start time. The vehicle uses it's accelerometers to measure the accumulated $\Delta v$ and stops the burn when this reaches the desired value. This is a much simpler task for the flight software than is a full-blown positional inertial navigation. The flight software doesn't need to know where the vehicle is, or even how fast it's going. It just needs to know how much its velocity has changed, as sensed by the accelerometers.
During the cruise phase of the mission, the onboard system typically does not keep track of position. Estimating and maintaining the vehicle's translational state is the job of the people on the ground who remotely monitor and control the vehicle. Both NASA and India have deep space networks that communicate with their interplanetary vehicles. One of the jobs of these deep space networks is estimating vehicles' orbits.
The time lag between transmission of a signal to a vehicle and the reception of the vehicle's response gives a good estimate of the distance (range). The doppler shift in the received signal gives a good estimate of the rate at which the distance is changing (range rate).
You might think that where those ground-based antennas have to point to communicate with the spacecraft would also give a clue as to where the spacecraft is. That's not the case, at least not with a single ground station. The spacecraft would be so far off course as to be unusable if the antenna had to be aimed in a measurably different direction than expected. The only useful measurements from a single ground station are range and range rate.
Angular measurements are possible during the short periods of time when the spacecraft is simultaneously visible to two ground stations. During those intervals, NASA uses very long baseline interferometry (VLBI) techniques to create what is equivalent to a single antenna that is thousands of kilometers across. Those VLBI measurements provide angular measurements in addition to the range / range rate measurements provided by a single antenna.
The point of taking these measurements is that the vehicle is never in exactly the orbit it should be in. The spacecraft's orbit can be re-estimated after collecting a number of these ground-based measurements of the spacecraft's state. The process is somewhat similar to the Kalman filter used in the spacecraft's flight software. A Kalman filter processes measurements once and then discards them. The batch least squares filters used by ground controllers process all of the data at once, and then can reuse them to even further refine the estimate of the orbit.
Because the spacecraft is never in exactly the right orbit, ground controllers will occasionally command the spacecraft to perform the previously mentioned trajectory correction maneuvers to put the spacecraft back on course toward the target.
For the relatively rare missions to other planets I would assume the satellites are almost constantly tracked. Either way we can quite easily predict the future position of any object in orbit around a central body with a very high accuracy, for a short period of time. So assuming regular checks are made to negate the non conservative perturbations of the orbit, we can just use astrodynamics.
A point of interest is that some of the non conservative perturbations will be practically non excitant in interplanetary (drag for example), others may be increased (solar pressure may be greater outside of the Earth's magnetic field, but smaller further from the sun).
Well star trackers take pictures of space and try and match that to a star chart; essentially saying if I've taken a picture of that area of the sky I must be facing this way. Sun sensors are a lot simpler, we're pretty confident of the solar flux at any given distance from the sun, so if we are at mars we know which part of the spacecraft is pointing at the sun based on the brightness. You also have horizon trackers, these look toward the planet and find the outline of the planet based on the difference between the dark background of space and the planet's surface. There are also gyroscopes that can be used to measure your attitude.