The car appears to have spent quite some time driving around the estate after having ditched Harry and Ron (following the unfortunate incident with the Whomping Willow). When it detects that they're in trouble, it comes to their aid. Note that the car has an easy familiarity with the path in and out of the forest.
Since the car belongs to Ron's father (and may or may not contain parts from Sirius Black's motorbike), it's hardly surprising that it would rescue them when the need arose.
Mr Weasley’s car was thundering down the slope, headlamps glaring, its
horn screeching, knocking spiders aside; several were thrown onto
their backs, their endless legs waving in the air. The car screeched
to a halt in front of Harry and Ron and the doors flew open. ‘Get
Fang!’ Harry yelled, diving into the front seat; Ron seized the
boarhound round the middle and threw him, yelping, into the back of
the car. The doors slammed shut. Ron didn’t touch the accelerator but
the car didn’t need him; the engine roared and they were off, hitting
more spiders.
They sped up the slope, out of the hollow, and they were soon crashing
through the Forest, branches whipping the windows as the car wound its
way cleverly through the widest gaps, following a path it obviously
knew.
Chamber of Secrets - Chapter 16
As to why JKR chose to have them rescued by the car (e.g. as opposed to a different deus ex machina), it's because she personally associates that make of car with a joyous sense of freedom:
The Flying Ford Anglia was based on a real car driven by Séan Harris,
the first of my friends to learn to drive. We were at school together.
At the time I lived in the middle of nowhere and had to rely on my
parents for transport. Sometimes Séan would come for me in his car.
The sight of that car meant freedom to me…my heart still leaps
whenever I see a Ford Anglia! That’s why , when I thought of Ron’s
flying car, I immediately pictured a Ford Anglia. Renton, Jennie. "Wild about Harry." Candis Magazine November, 2001