The hosts were well off the pace in the first half and fell behind to a fine team goal from Sassuolo two minutes before the break, with Davide Frattesi finishing well when put through on goal.
The equaliser finally came when American Weston McKennie powered a header into the net in the 76th minute, much to the relief of everyone in the Juventus Stadium.
Juve threw everything at Sassuolo to find a winner but they were caught on the break in the 95th minute as Lopez raced clear before lofting a fine finish into the net to secure victory.
The surprise defeat, Juve's first in 10 matches in all competitions, dropped Allegri's side to seventh in the standings with 15 points. They trail leaders AC Milan by 13 points after 10 games. Sassuolo climbed to ninth, a point adrift of Juve.
"There are games that if you don't win you can't afford to lose," Allegri told DAZN. "A draw would have meant we extended the unbeaten run and we would not have lost twice at home (this season) already.
"We have to be realistic. We have to play in another way and be more careful, especially if you meet teams like Sassuolo."
Allegri challenged his side to get among the goals against Sassuolo, with Juve having not scored more than once in a match in their previous five encounters.
But even with forwards Alvaro Morata, Chiesa and Paulo Dybala all starting, they struggled to create many clear cut chances.
Dybala did almost break the deadlock with a fizzing strike from distance that clipped the outside of the post in the first half, before Frattesi made his moment count.
Visiting goalkeeper Andrea Consigli was keeping Juve at bay, as were several last-ditch defensive interventions, before McKennie's first of the season levelled things up, on the end of a pinpoint Dybala cross.
Juve pressed for that winner but with everyone committed down the other end of the pitch, they were left short at the back as Sassuolo picked up their first-ever win in the Juventus Stadium thanks to Lopez's calm finish under pressure.