Sandoval, whose future with the team is uncertain, hit a 3-2 fastball from rookie Phil Maton (3-2). It was his fifth homer in 47 games since returning to San Francisco this summer.
Giants reliever Hunter Strickland (4-3) pitched 1 1/3 scoreless innings.
"On your last game and at home here, you want to win the game, but how we won it, in that kind of fashion, it was a great ballgame," Giants manager Bruce Bochy said.
"For (Sandoval) to hit a home run, to walk it off, I know that's special for him and it was cool to see because he's done a nice job since coming here.
"It brought back good memories, I'm not going to lie."
After the game, San Francisco honored retiring pitcher Matt Cain on his 33rd birthday. Cain made the final appearances of his 13-year career Saturday. Bochy and catcher Buster Posey both spoke, and Bochy also thanked the fans for supporting the Giants through a 64-98 season.
Sandoval, who wants to return to San Francisco next year, made a strong closing argument Sunday.
"I feel excited for that moment, especially when I was running the bases, I wanted to cry," said the 2012 World Series MVP.
"I want to be here for the rest of my career. I want to be like Cain," he added
The Giants lost 23 more games than they did last season in their worst year since 1985, when they lost 100 games.
They finished tied with the Detroit Tigers for the worst record in baseball. The Tigers will get the first overall pick in the 2018 amateur draft under MLB's tiebreaking rules awarding the first pick to the team with a worse record the previous season.
The Padres (71-91) lost for the sixth time in seven games as they concluded their seventh straight losing season with a modest improvement of three more wins than last season.
The Giants hadn't won a series with the Padres since the 2016 All-Star break, losing seven of eight with one series tie over that stretch.
Padres starter Luis Perdomo, who's expected to compete for a spot in the rotation next spring, gave up four runs, seven hits and one walk in seven innings. The 24-year-old right-hander threw a season-high 108 pitches.
Hunter Renfroe was 2 for 4 with a home run. He connected on his 26th homer off Cueto in the third inning.
"I'm looking forward to a little relaxation period and then pick it up in spring training," Renfroe said. "We're hoping to make the playoffs next season, that's our biggest goal."
Giants right fielder Hunter Pence made a diving backhand catch on a ball in the gap, robbing Renfroe of extra bases for the final out in the seventh. Pence left the field to a standing ovation. He came out of the game with an unspecified injury, Bochy said.
Perdomo tripled leading off the fourth inning, his fifth of the year and his second against the Giants. Perdomo scored when Travis Jankowski followed with an infield hit to give the Padres a 4-1 lead.
More AP baseball coverage: https://apnews.com/tag/MLBbaseball
Patrick Mahomes inherited clutch gene from his major-league dad
Can the Chiefs quarterback remain composed as the postseason stages get bigger? His family history might give him a distinct advantage.
Five moves we want to see this offseason
Corey Kluber and A.J. Pollock turning the Reds into contenders? A swap of former No. 1 prospects? Dallas Keuchel teaming up with Manny or Bryce? Here's how we're hoping the rest of the hot stove season plays out.
Anaheim votes in favor of 1-year lease extension for Angels
The City of Anaheim voted in favor of a one-year extension of the Angels' stadium lease. The club will play in Angel Stadium through 2020.
Champion Red Sox to visit White House during spring training
The World Series champion Boston Red Sox will visit the White House on Feb. 15 and will also visit the Walter Reed National Military Medical Center during the trip.
Why are Machado and Harper still unsigned?
Tony Kornheiser and Mike Wilbon disagree on why Manny Machado and Bryce Harper remain unsigned into mid-January.
Matt Shepard taking over play-by-play for Tigers' TV broadcasts
Matt Shepard is taking over as play-by-play announcer for the Detroit Tigers on their Fox Sports Detroit television broadcasts.