Hunter Pence, who reached a tentative agreement on a $90 million, five-year contract before the game, hit a two-run homer for the Giants. San Francisco had won three in a row.
Eric Stults (11-13) pitched seven effective innings to win his third straight start after going 0-6 in his previous 10 starts. He also helped himself with an RBI single during a four-run fourth.
Stults allowed three runs on seven hits. He did not walk a batter and struck out three.
Yusmeiro Petit (4-1) lasted a season-low 3 2-3 innings, giving up five runs and seven hits.
Luke Gregerson pitched the eighth and Huston Street worked the ninth. The Padres are a game ahead of the Giants in third place in the NL West.
Denorfia led off the game with his career-best 10th home run.
Angel Pagan doubled to open the bottom of the first and scored when Sandoval doubled with two outs.
Pence hit his 27th homer in the third to put the Giants ahead 3-1. His homer total is the highest for the Giants since Barry Bonds hit 28 in 2007.
Petit retired the first two batters in the fourth and then gave up six consecutive hits before being replaced by George Kontos.
Chase Headley doubled and Tommy Medica singled following a 14-pitch at bat. Guzman hit a two-run homer and consecutive singles from Alexi Amarista, Chris Robinson and Stults produced another run and ended Petit's day.
Stults, who retired the last nine hitters he faced, threw a career-high 203 2-3 innings.
The Padres added four runs in the ninth on Gyorko's 22nd home run, a two-run shot, and RBI singles from Denorfia and Reymond Fuentes.
Gyorko has the most home runs by a rookie second baseman since Dan Uggla hit 27 for the Marlins in 2006.
NOTES: Giants INF Brandon Belt will be working on his versatility during the offseason according to manager Bruce Bochy. ... RHP Guillermo Moscoso (2-2, 4.21) will start Sunday's season finale for the Giants. It's his first start since Aug. 4. ... RHP Tyson Ross (3-8, 3.10) makes the start for the Padres on Sunday. He has an ERA of 2.80 over his last 12 starts. ... Denorfia hit his second leadoff homer of the season and seventh of his career. ... Medica had his first career three-hit game.