Mauro Icardi's first-half goal was enough for PSG to edge past Club Brugge 1-0 at the Parc des Princes, as Bayern laboured to a 2-0 victory over Olympiakos.
Manchester City missed the chance to progress from Group C after a dramatic 1-1 draw at Atalanta which saw right-back Kyle Walker end the game in goal.
Juventus knew three points in Russia would be enough to secure their passage to the knockout rounds, and took a fourth-minute lead through Aaron Ramsey.
Cristiano Ronaldo's free-kick bounced in front of Lokomotiv goalkeeper Guilherme, who inexplicably allowed the ball to squirm through his legs, with Ramsey prodding in from on the goalline.
Aleksei Miranchuk quickly levelled for the hosts by tapping in the rebound after his own header had struck the post, with Maurizio Sarri's Juve then struggling to break their hosts down.
But Brazilian winger Costa came off the bench and netted a magnificent 93rd-minute winner, slipping the ball through the hapless Guilherme's legs after a mazy run.
"He (Costa) is returning to top condition after an injury but he has enough quality to play anywhere on the pitch," said Juve coach Sarri.
Icardi fires PSG through
PSG reached the last 16 for the eighth straight season, but a nervy display against Brugge will not have boosted hopes of the French champions making the semis for the first time since 1995.
Icardi swept Thomas Tuchel's men into the lead midway through the first half with his eighth goal since joining on loan from Inter Milan.
But Belgian outfit Brugge should have grabbed a late equaliser, only for Mbaye Diagne to see his penalty saved by PSG 'keeper Keylor Navas.
"It is logical we are annoyed after a game where I think we deserved something but you still end up empty-handed," said Brugge goalkeeper Simon Mignolet.
Managerless Bayern Munich had earlier become the first side to go through, as late goals from Robert Lewandowski and Ivan Perisic ended Olympiakos' resistance at the Allianz Arena.
"I'm very happy with the performance, we're into the last 16 and that's what counts," said Hansi Flick, who took over as interim coach after Niko Kovac was sacked on Sunday.
Bayern lead Tottenham at the top of Group B by five points, after last season's runners-up thrashed Red Star Belgrade 4-0 in Serbia.
Son Heung-min scored twice just three days after being left devastated by his tackle which caused a horrific injury for Everton's Andre Gomes, with Giovani Lo Celso and Christian Eriksen also finding the net.
Goalkeeper Walker helps City to point
Premier League champions Man City were made to wait after Atalanta's second-half fightback at the San Siro.
Raheem Sterling, who scored a hat-trick in City's 5-1 victory in the reverse fixture two weeks ago, put Pep Guardiola's men in front after only six minutes, but Gabriel Jesus dragged a dreadful penalty well wide shortly before half-time.
City goalkeeper Ederson was replaced by Claudio Bravo at the break as a precaution ahead of their crucial Sunday clash at Premier League leaders Liverpool.
Chilean Bravo saw Mario Pasalic's header fly past him in the 49th minute, but worse was to come when he was sent off with nine minutes remaining for hacking down Josip Ilicic.
Walker was brought on to play as 'keeper, and despite almost fumbling Ruslan Malinovsky's effort from the resulting free-kick into his own net, helped City secure a point.
"I say again, thank you Kyle because it is not easy to take this responsibility to go on and be a goalkeeper and save the free-kick," said City full-back Benjamin Mendy.
Atletico Madrid also missed out on booking a last-16 place as they slumped to a 2-1 defeat at Bayer Leverkusen.
A bizarre Thomas Partey own goal and Kevin Volland's second-half strike kept the Germans in contention to finish second behind Juventus in Group D despite Alvaro Morata's late consolation.
Shakhtar Donetsk snatched a thrilling 3-3 draw at Dinamo Zagreb thanks to two injury-time goals, including a 98th-minute penalty from Tete.
Those two teams remain level on five points in Group C behind Man City, but Shakhtar are second due to having scored more away goals in their head-to-head matches.