A phone camera can keep a moving subject in focus because every part of its sensor reports which way the picture is blurred and by how much, in a single frame. It moves the lens by that amount before the subject has gone anywhere else, then repeats the reading about 30 times a second. Figures below describe an invented camera and a cyclist riding towards it, so your phone will differ. Because the reading needs no search, the lens can start moving at once. A short answer comes first, and the cyclist follows, frame by frame.

How a phone camera can keep a moving subject in focus
The short answer is that each frame the camera measures how far and in which direction the image is out of focus, then nudges the lens to cancel that error. The measuring is done by the sensor itself. Under each tiny lens on the sensor sit two light detectors, one fed by light from the left half of the main lens and the other by light from the right half. Sharp pictures match. Blurred ones slide apart by an amount that gives the size of the error.
Direction comes free with the size. If the left picture has slid to the left, the lens is too far in one direction, and if it has slid right, the lens is too far the other way. The camera needs no trial and error for that.
That summary leaves out two things. It stays silent on how the camera copes while the subject keeps moving between readings, and it skips what happens when the light is poor. The cyclist below deals with the first, and the section on limits deals with the second.

What a lens must do to keep a moving cyclist in focus
Take an invented camera with a 6 mm lens at f/1.8. Its pixels are 1 µm wide, and it shoots 30 frames a second. A cyclist rides straight towards it from 12 m at 6 m/s, so she closes 0.2 m between frames. The lens must sit a tiny distance beyond its far-focus position for the image to be sharp: 3.0 µm at 12 m, 6.0 µm at 6 m, 12.0 µm at 3 m and 36.2 µm at 1 m. So it creeps outwards slowly at first and quickly at the end. Between 6 m and 3 m, which takes half a second, it travels 6 µm, double what it needed for the first 6 m of her ride.
Each frame, the sensor compares the left and right pictures. Suppose the lens is 9 µm out of place. The two pictures then sit 9 ÷ (2 × 1.8) = 2.5 µm apart, about 2.5 pixels, and the sign says which way to move. One reading gives direction and distance together, so the motor makes one move instead of a search.
Frame by frame, the camera also builds a trend. At 3 m the sharp position changes by 0.86 µm per frame, so it can predict where the position will be two frames later. That matters because the motor takes about two frames to respond. The camera aims for where the cyclist will be, not where she was.
How late may it be? The tolerance is ±3.6 µm (1.8 × 2 µm, a blur of two pixels). At 3 m the sharp position moves 0.86 µm a frame, so a lag of up to four frames, 0.14 s, still gives a crisp picture. At 1 m it moves 9.1 µm a frame, and the allowed lag shrinks to 0.4 of a frame, 13 ms. A rider close to the lens is the hard case.
Searching by contrast fares worse. It tries lens positions until the picture looks sharpest, eight steps at one a frame, so 0.27 s with no hint of direction at the start. That is twice the four-frame allowance at 3 m.
Catching a high ball, and where the comparison breaks
A fielder under a high ball does not run to where the ball is. She runs to where it will land, and she adjusts as she sees it fall. Tracking focus follows the same pattern. Tracking reads where the sharp position is and extends the trend. Then it sends the lens to the spot where it expects the cyclist to arrive.
The comparison breaks on what each one knows about the world.
The fielder understands how gravity bends a path and that a ball won’t suddenly double back. Our camera holds only one number per frame and a straight-line guess. If the cyclist swerves or stops, the guess is wrong until the next reading arrives, and the lens spends those frames moving the wrong way.

What autofocus cannot do for a fast subject
The camera follows a patch of picture, not the person you care about, so anything that crosses that patch can take the focus away. A lamp post passing between you and the cyclist is enough. The patch’s texture now belongs to the post, the readings jump to 2 m, and the lens swings to match them.
Dim light is the second limit. A reading needs enough light to build two clean pictures, and in a dark street each detector collects few photons, so the two pictures grow noisy and the shift is hard to measure. The camera then falls back on the slower search by contrast. A blank wall causes the same trouble, because it gives the detectors nothing to compare.
Speed is the third limit, and it bites hardest at close range.
At 1 m our cyclist leaves a margin of only 13 ms. No autofocus can keep a moving subject in focus at that range without some luck, because the motor needs about two frames to respond.
What people get wrong about autofocus tracking
Many people believe the camera knows what a cyclist is. That belief forms because the screen draws a neat box around a rider, which looks like recognition. The box only marks a patch of texture, and a tap merely chooses which patch to follow, so it can keep a moving subject in focus only while that patch stays clear. The patch is all it follows, so a similar-looking rider in the same box can take over.
Others think a dark scene is slow because the lens motor is slow. It forms because the delay appears when the lights go down. The real delay is in the reading, since noisy pictures need more frames before the shift can be trusted.
A third belief holds that searching by contrast is the old, worse method. It forms because the hunting is easy to see. Phones still use it as a final check once the fast reading has put the lens in the right place.

How to decide when to let the camera track and when to pre-focus
The rule is to set focus on the spot where the subject will pass and shoot as it arrives whenever it will come within 1 m or the light is dim. Otherwise let tracking run. Our cyclist at 3 m or more, in daylight, gives the camera about four frames of slack, so tracking copes. At 1 m the slack is 13 ms and you can’t rely on it. Pick a mark on the ground at the right distance. Hold focus there, then take a burst as she reaches it. Pacing out the distance and finding the burst setting take a minute.
To keep a moving subject in focus, give the camera good light and a clear patch of texture to follow. When it can’t help, give yourself a mark on the ground instead. A burst costs a little storage, whereas a blurred shot of a moment that won’t repeat costs far more. Try the rule once on a friend walking towards you, and you’ll learn your phone’s limit in minutes. A mark chosen in advance also saves you from guessing during the moment itself.
Questions readers keep asking
Why does the focus sometimes pulse back and forth?
The camera has lost the fast reading and fallen back on the search by contrast. Each lens position makes the picture sharper, then softer, then sharper again. It usually happens in dim light or on a plain surface. Move to better light or aim at something with edges and it settles.
Do I need a special tracking mode?
Usually not. Most phones track by default once you pick a subject. A separate mode matters only for fast rides and running pets, where the distance changes quickly. For a still face or a slow walk, the ordinary setting gives the same sharp result.
Is this the same as focus on a bigger camera?
The principle matches, since larger cameras also compare two views of the subject. Heavier lenses make the motor slower, while a larger sensor gathers more light. A phone’s tiny lens needs only micrometres of travel, which makes its focus quick in good light.