In this scene from Back to the Future Part II Marty is about to be run over, but Doc saves him.
How did Doc Brown know to drop the rope? (I read a fan hypothesis here.)
The simple answer is that the Doc saw Marty and Biff go into the tunnel, with Marty hanging onto the side of Biff's car. He then flew over the top of the mountain to sit at the other end.
When he didn't see them come out, he rightly surmised that Marty must have let go of Biff's car and would be being chased by Biff back the way he came.
From the film's official novelisation;
So where was Marty?
Doc had kept the DeLorean hovering over the exit to the tunnel for the last minute, expecting Biff’s car to show up at any second. But there was no sign of Biff, and no sign of Marty - no sign of anything, really, except a big old truck carrying fertilizer that had gone into the tunnel beneath him.
Gone into the tunnel?
Oh no, Doc Brown thought, Marty didn’t have to come out this way at all.
He raised the DeLorean quickly. He only hoped he wasn’t too late to rectify his error.
There is another explanation, which is a dark, uncanon theory:
Marty actually dies and doc finds it out later and goes back in time to rescue him. And perhaps that's not even the first time that happens either! Remember when Biff points a gun to Marty in 1985A? Marty jumps off the building and stands over the flying DeLorean. Maybe he died once. How did Doc Brown know where Marty was anyway? Deus Ex Machina?