Rees nearly rallied the Fighting Irish (8-4) by throwing two touchdown passes later in the quarter. But interceptions on Notre Dame's final two drives dashed Notre Dame's come back.
Stanford relied on its power running game to control the tempo throughout, but blew several chances to pull away.
Gaffney plowed through the Irish line from a yard out to give the Cardinal a 14-3 lead early in the second quarter. The bunched formation was similar to the one Notre Dame stopped Stepfan Taylor on last year to win in overtime, a stinging loss that the Cardinal regrouped from to roll off eight straight victories, including the Pac-12 title and the Rose Bowl.
Backup Anthony Wilkerson capped the opening drive of the second half in similar fashion. He ran for a 20-yard TD run on third-and-9 to put Stanford ahead 21-6.
Notre Dame drove deep in Stanford territory on its first and final drives of the first half before settling for field goals each time. With Stanford seemingly ready to turn the game into a rout, Rees rallied the Irish by throwing touchdown passes to TJ Jones and DaVaris Daniels late in the third quarter.
Mistakes nearly cost the Cardinal late.
Stanford had no problem sustaining drives but struggled to finish them with touchdowns in the second half, twice settling for field goals. On one of them, a holding penalty on right guard Kevin Danser — only the second one by a Stanford offensive lineman all season — erased a touchdown rushing for Gaffney.
Hogan threw his second interception of the game when Bennett Jackson came down with his pass at Notre Dame's 21 with 6:23 remaining. Austin Collinsworth also intercepted a pass from Hogan in the first half.
Just as it has so many times over the past two seasons, the Cardinal defense bailed out the offense in the end.
Lyons intercepted Rees' overthrown pass at Stanford's 41, the 36th consecutive game the Cardinal have forced a turnover. And after the Irish stopped Stanford three-and-out, Lyons leaped high to intercept Rees' pass again on Stanford's 30 with 2:24 left.