The race was animated by a long-range attack by Alberto Contador (Trek-Segafredo) with the Spaniard on the offensive from well over 100km out.
Contador set the race alight in Stage 17, attacking with 125km left in the stage and using his teammates to bridge up to the early breakaway and drive the break, but he didn't have the legs to go with Roglic when the Lotto NL-Jumbo rider launched his assault.
Roglic had done a lot of work to shut down attacks throughout the climb and when he went on the attack himself it was clear that there would be no catching him.
"It's crazy, huh," said Roglic. "Incredible... there are no words. Right now I can't work out all my feelings but later I'll know how big this is."
"I decided to go for it finally today, my girlfriend and my family were here. When I saw now all these Slovenian flags, all my neighbours and family around... it's just crazy."
Behind Roglic, the race between the contenders for yellow heated up on the legendary ascent of the Galibier. Numerous attacks were launched, with Romain Bardet (AG2R) the main aggressor.
Chris Froome (Sky) retained yellow after proving equal to the forays off the front by the other favourites, even slightly extending his advantage after second-placed Fabio Aru (Astana) was dropped from the front of the race.
As it happened
Stage 17 was a day of stunning alpine scenery, hard racing and a visit to the race by new French President Emmanuel Macron.
An aggressive start to the stage saw a lot of riders keen to get into the break and try their luck over the hard mountainous terrain of the French Alps. The peloton wasn't keen to let anything go easily at the start of the race, until a crash at the back of the bunch saw a slowing of the pace that allowed a group of 30 to go clear.
— CyclingCentral (@CyclingCentral) July 19, 2017
A number of riders were caught up in the crash, with Marcel Kittel (QuickStep Floors) the highest profile casualty. The winner of five stages struggled on some way into the mountains but ended up abandoning the race, leaving Australian Michael Matthews (Sunweb) in the pole position for the green jersey.
Matthews himself was part of the large move that went away early, looking to get himself more points against the big German in the fight for the maillot vert. With the break disappearing up the road with an advantage of five and a half minutes, Nairo Quintana (Movistar) launched a short-lived attack. Just as he looked like he would be brought back by the Sky-led peloton, Contador launched his own move. Going clear of the main bunch, Quintana in tow, the former Tour de France champion began a long move with over 125km remaining in the race.
Quintana initally kept Contador's wheel, with the veteran even waiting for his younger rival to catch up when dropped. Eventually, the out-of-form Quintana simply couldn't hold the pace and it was just Contador away by himself with still a few minutes to bridge to the early escapees, which included three of his teammates.
Team-mate Michael Gogl dropped back to help Contador, getting him closer to the breakaway group before dropping him off within sight of the back of the front group.
Contador made the group and then set about having teammates Pantano and Mollema drive to pace to aid his charge up the general classification.
He had started the stage seven minutes and 10 seconds down on the yellow jersey and the efforts of his Trek-Segafredo team kept his gap to the Sky-led main bunch consistently around the three to four-minute mark.
The consistently high pace of the peloton and the breakaway saw riders dropped off the back as the imposing climbs of the Croix de la Fer and the Col du Telegraph took their toll.
Stage 16-winner Bauke Mollema (Trek-Segafredo) set a hard tempo for Contador up the Col du Telegraph and the early slopes of the Col du Galibier until Roglic jumped away with 44km left to race. He was quickly joined by Contador and Serge Pauwels (Dimension Data).
Behind the front trio, a fight back came from the group behind, with Matthias Frank (AG2R), Darwin Atapuma (BMC) and Dani Navarro (Cofidis) bridging across to form a strong group of six. Pauwels was the first to try and get away from the lead group, looking for the stage win as the peloton continued to decrease the leaders' advantage.
Roglic was the main man leading the chase to Pauwels and once the Belgian was caught, he counterattacked and went clear solo at the head of the race.
Dan Martin (Quick-Step Floors) launched an attack from the peloton, trying to make up a significant dent into his two-minute deficit to the yellow jersey. He dangled off the front of the group, before getting recaptured as Mikel Landa (Sky) went to the front and dragged him back.
Bardet went on the attack with 32km left forcing all the favourites to scramble for wheels. Froome seemed strong enough to deal with the attacks, but second-placed Aru was consistently put in trouble by the accelerations of the other contenders.
A final attack by Bardet to the summit of the Col du Galibier dropped a number of riders with only Froome, Landa, Dan Martin (Quick-Step Floors), Rigoberto Uran (Cannondale-Drapac) and Warren Barguil (Sunweb) able to get on terms. Contador was swept up and passed as well, though he wouldn't go home entirely empty-handed, taking out the combativity prize for the stage.
Aru struggled to make up the gap to the other contenders, taking risks on the descent of the Col du Galibier, ignoring the treacherous drop-offs from the side of the road. He looked set to make contact, but a further acceleration by Bardet dropped all but Froome, Landa, Uran and Barguil.
Roglic continued his individual effort off the front of the race and the time-trial specialist did a superb job to concede only a few seconds to the chasing group of favourites. He went over the finish line arms aloft as the jousting for the final sprint began behind.
With valuable bonus seconds on the line, it was Uran who crossed the line for second and the six-second bonus, with Froome claiming the four-second bonus for third. The group of Aru conceded 30 seconds to the yellow jersey, while Simon Yates (Orica-Scott) gave up a more time still, finishing just over two minutes down on the group of Froome, also conceding a minute and a half to white jersey rival Louis Meintjes (Dimension Data).