The Six Nations champions' 20-19 victory was secured by Ross Moriarty's 74th-minute try that Dan Biggar converted.
There was an agonising wait while the officials checked for a possible Wales knock-on in build-up play, but the try was awarded.
A Wales win looked unlikely for long periods as an often flamboyant and brilliant French team dominated.
But the game changed course after Les Bleus lock Sebastien Vahaamahina was sent off in the 48th minute for elbowing Wales flanker Aaron Wainwright in the face.
Wales trailed by nine points at the time, but they dug deep after an earlier Wainwright try, while Biggar booted 10 points.
France were breathtaking at times, showcasing their beguiling best as Vahaamahina, flanker Charles Ollivon and centre Virimi Vakatawa scored first-half tries.
Wales conceded two touchdowns during the first eight minutes, had Moriarty sin-binned for a high tackle, yet they somehow got over the winning line.
Wales head coach Warren Gatland steps down after the tournament following a 12-year reign highlighted by three Six Nations Grand Slams.
And Wales' World Cup adventure will now continue for at least another week, with Japan or South Africa their semi-final opponents.
It was the eighth time in nine attempts since France knocked Wales out of the 2011 World Cup for Wales to defeat Les Bleus.
And it was a win that was underpinned by their fearless resolve, never-say-die attitude and unshakeable self-belief.
Wales suffered a major blow ahead of the game when their British and Lions centre Jonathan Davies was ruled out after aggravating a knee injury.
Owen Watkin moved off the bench to take over in midfield, with Leigh Halfpenny joining the replacements.
Wales were rattled by an aggressive French start as their big ball-carriers made headway.
It was all hands to the pump for Wales' defenders, but France went ahead after five minutes when Vahaamahina powered over for a try from close range.
And before Wales could recover from that setback, they fell further behind after a brilliant France move from inside their own half saw scrum-half Antoine Dupont deliver a scoring pass to Ollivon.
Fly-half Roman Ntamack converted Ollivon's score, and France were rampant, leading 12-0 after just eight minutes.
Wales had to reply quickly following such an unsettling opening, and they delivered courtesy of a 12th-minute try for Wainwright.
The Dragons forward, who had never previously scored for his country, gathered a loose ball and then galloped clear from 30 metres out to touch down, with Biggar's conversion making it 12-7.
It was a strong response, and then Biggar kicked a penalty to leave Wales just two points adrift after 20 minutes.
Wales appeared back in the contest, and they began to shade the territorial battle, but the Six Nations champions then suffered an injury blow when flanker Josh Navidi went off and was replaced by Moriarty.
But Moriarty had not been on the pitch for two minutes when referee Jaco Peyper yellow-carded him for a high tackle on France centre Gael Fickou.
Moriarty's exit left Wales at sixes and sevens, and France punished them in ruthless fashion when Vakatawa crossed for their third try.
Ntamack converted to make it 19-10, and 14-man Wales were left hanging on, and they had a mountain to climb as Moriarty returned.
Ntamack went off at half-time and was replaced by Camille Lopez, but Vahaamahina was then sent off following a crass act of stupidity that left Peyper with no choice but to brandish a red card.
A Biggar penalty cut the gap to six points, and then came pulsating late drama as Wales triumphed.