Stanford Stanford (7-1, 5-1 Pac-12) took a 20-9 lead on Gaffney's third touchdown of the game, a 32-yard run in the fourth quarter.
Minutes later, Gaffney but fumbled at the Cardinal 20 and the Beavers (6-2, 4-1) made it 20-12 on a 39-yard Trevor Romaine field goal.
Oregon State quarterback Sean Mannion hit Kevin Cummings with a pass in the end zone but Cummings coughed it up on a hit by Stanford safety Ed Reynolds with 7 seconds left to play.
Pregnant Mila Kunis wins 'Best Villain' at MTV Movie Awards
Pistorius testifies he didn't consciously pull trigger when he shot girlfriend