As others have stated, the way to keep track of your position when you can't use GPS is inertial navigation, also known as dead reckoning. This uses various sensors, such as accelerometers, magnetometers and gyroscopes, which form an IMU (inertial measurement unit).
The issue is that it is by definition subject to drift (so with time the estimated position can get further and further away from the actual position), and that it's even more complex on a hand-held device such as a mobile phone as opposed to a car.
On a car, you can get (if the GPS is properly wired into the car system) two bits of information that are quite handy:
- The orientation of a car (from a magnetometer which is basically an electronic compass)
- The distance traveled (by counting wheel turns and knowing the wheel size -- this is how your mileage counter works).
Each wheel turn, you add the size of perimeter of the wheel to your previous position in the direction of the orientation of the car.
This can give you a pretty accurate result. You will get slightly off if you turn a lot (or if the tire size doesn't match!), but the error should remain quite manageable.
If your GPS cannot get the wheel turns information (i.e. it's an add-on device, not wired into the car), then it get slightly more tricky, as you can't directly get a distance traveled. Instead, you rely on accelerometers, which will give you... acceleration (it's the only thing you can measure directly).
From acceleration, by integration, you can get speed: if you start at 0 m/s and measure a constant 1 m/s^2 acceleration, after one second your speed is 1 m/s, after 2 seconds it's 2 m/s, and so on. If acceleration drops to 0, then the speed remains constant.
From speed, you can get distance traveled via the same mechanism. Double integration means that your position is based on the square of elapsed time (x = x0 + 0.5.a.t^2 if acceleration was constant). This means that even a slight error in measurement of acceleration will quickly grow to become a quite large error in the measurement of distance travelled. To minimise error, you need to have the lowest possible error in measurement (better sensors), and perform measurements as frequently as possible (we're talking fractions of a second).
With this, it's still possible to get a decent estimate, especially as a car doesn't (usually) swerve around a lot, or have lots of variation in acceleration. The magnetometer being fixed in the car means that it can tell you quite precisely where the car is headed.
However this all goes awry when you consider a mobile phone. You have it in your hand. Try as you may, you will at the very least bounce it slightly left and right and up and down as you walk. You probably don't have a constant speed either, so you'll also impart a slight acceleration front and back. This means acceleration changes a lot in all 3 axes. Orientation as well.
This results in a lot more difficult measurement, will lots of errors, especially if you don't sample acceleration and orientation and rotation often enough. Modern IMUs will do that all inside the chip to try to get the best estimate possible, but that's a pretty difficult task.
Wikipedia reminds us that maths dictates that:
To get a rough idea, this means that, for a single, uncorrected accelerometer, the cheapest (at 100 mg) loses its ability to give 50-meter accuracy after around 10 seconds, while the best accelerometer (at 10 µg) loses its 50-meter accuracy after around 17 minutes.
And that's just based on pure acceleration, not even taking into account rotation of the phone.
Not all phones are created equal in that respect, and some will have better performance than others (because they have more recent, more expensive IMUs with less errors, higher frequency), but in any case, it won't (and can't) give you a precise position for a very long time: they need to be frequently re-synchronised to their actual position through other means (GPS or Wi-Fi/cellular trilateration).