The 26-year-old led the standings by 25 seconds heading into the final stage time trial, but was cruelly denied by the Slovenian at the last.
Yates was always going to be tested by the TT specialist on the 10.5-kilometre course along the seafront of San Benedetto del Tronto, and so it proved as he finished 26 seconds down on his rival.
"It was a hard TT. I've done this TT many times and I always lose a chunk of time and it was close but it wasn't enough," Yates said. "That's how it goes, that's bike racing."
Belgium’s Victor Campenaerts took victory in 11:23 on Stage 7 ahead of Italy’s Alberto Bettiol and the Netherlands’ Jos van Emden.
"It was unbelievably close. I was lucky in the end," Roglic said. "I really pushed until the finish line. I was confident but I can only control myself."
Roglic delighted to edge narrow finish
Subscribe to The Bradley Wiggins Show by Eurosport
Stage 7 results
1 Victor Campenaerts (Bel) Lotto Soudal 0:11:23
2 Alberto Bettiol (Ita) EF Education First 0:00:03
3 Jos van Emden (Ned) Team Jumbo-Visma 0:00:04
4 Sebastian Langeveld (Ned) EF Education First 0:00:06
5 Yves Lampaert (Bel) Deceuninck-QuickStep 0:00:07
6 Mads Pedersen (Den) Trek-Segafredo 0:00:08
7 Tom Dumoulin (Ned) Team Sunweb
8 Rohan Dennis (Aus) Bahrain-Merida 0:00:09
9 Michael Hepburn (Aus) Mitchelton-Scott 0:00:11
10 Filippo Ganna (Ita) Team Sky 0:00:12
Highlights - Roglic snatches victory on final stage
1 Primoz Roglic (Slo) Team Jumbo-Visma 25:28:00
2 Adam Yates (GBr) Mitchelton-Scott 0:00:01
3 Jakob Fuglsang (Den) Astana Pro Team 0:00:30
4 Tom Dumoulin (Ned) Team Sunweb 0:01:25
5 Thibaut Pinot (Fra) Groupama-FDJ 0:02:32
6 Julian Alaphilippe (Fra) Deceuninck-QuickStep 0:02:34
7 Wout Poels (Ned) Team Sky 0:02:42
8 Simon Clarke (Aus) EF Education First 0:03:01
9 Sam Oomen (Ned) Team Sunweb 0:03:12
10 Rui Costa (Por) UAE Team Emirates 0:03:18