The American teenager, ranked 313th in the world, had earned a match on Centre Court after capturing the public's imagination with her giant-killing exploits, including ousting former champion Venus Williams in the first round.
Gauff, who beat former semi-finalist Magdalena Rybarikova in the second round, was roared on by the crowd but found it hard at first to cope with 28-year-old Hercog's heavy groundstrokes and big serve.
She showed remarkable composure, however, to fight back against the 60th-ranked Slovenian, who failed to convert two match points in the second set.
"I always knew I could come back no matter what the score is, I just went for my shots," Gauff, nicknamed Coco, said after leaving the court to warm applause.
"The crowd was amazing. Even when I was down match point they were still cheering me on."
Hercog needed treatment on her back after the second set and slumped to 4-1 in the third but she too fought back in the tense duel.
"I’m relieved it’s over - she was playing unbelievably," Gauff said.
Hercog dumped a shot into the net in the final game and another went long under Gauff's pressure before the teenager leapt in the air to celebrate her victory as her proud parents applauded from the players' box.
Gauff is the youngest player to qualify for Wimbledon in the Open era and the youngest to reach the fourth round since American Jennifer Capriati, also aged 15, in 1991.
She faces seventh seed Simona Halep, a former world number one, in the last 16.