Brandon Crawford led off the Giants eighth with a single against Luis Frias (1-1) and advanced to second on Cooper Hummel's passed ball. One out later, Jason Vosler walked, and another passed ball moved the runners to second and third.
Pinch-hitter Wilmer Flores walked to load the bases and Reyes Moronta relieved Frias. Longoria's line single to left scored the Giants' first two runs after they had left 10 runners on base without scoring in the first seven innings.
"We need a miracle to make the playoffs at this point, but our message has been consistent. Just trying to continue to play hard," Longoria said.
The Giants have not been mathematically eliminated yet from the wild-card race because they went 6-1 on their trip to Colorado and Arizona. Their only loss was Saturday to Merrill Kelly and the Diamondbacks.
"It's been a long season and a lot of guys are tired, but the preparation has been excellent," Giants manager Gabe Kapler said.
Camilo Doval pitched the ninth for his 26th save, giving up a run, a walk and a hit. He struck out rookie Corbin Carroll to end it, throwing one pitch at 104 mph to match his fastest of the season on Friday.
Jakob Junis (5-6), who entered the game with two outs in the third, gave up four hits and one run in 5 1/3 innings. He struck out seven and walked one.
J.D. Davis had four hits for the Giants, including a solo homer off Mark Melancon into the swimming pool area in right-center in the ninth. The home run was Davis' 10th of the season, his sixth since being acquired from the New York Mets.
Arizona's Drey Jameson had his third strong start since he was called up from Triple-A Reno. He went 5 1/3 innings, giving up five hits and no runs. He won his first two starts against San Diego and Los Angeles and has a 0.98 ERA in 18 1/3 innings to begin his career.
Giants second baseman Ford Proctor, recalled from Triple-A on Saturday, recorded his first big league hit Sunday, a sharp single to left in the seventh inning off Ginkel.
BUM STOPS HERE
Diamondbacks manager Torey Lovullo said before Sunday's game that LHP Madison Bumgarner will not pitch again this season.
"He's perfectly healthy. We just have some younger arms that we want to get a look at," Lovullo said.
"He understood. Bum's a competitor, he wants to go out and work and help his team. He's a good teammate. He understood the rationale."
Bumgarner finishes 2022 with a 7-15 record, a 4.88 ERA in 30 starts, and a 1.437 WHIP. The 33-year-old left-hander has two years remaining on a five-year, $85 million contract he signed in 2020 after 11 seasons with the Giants that included three World Series titles.
In his last start on Thursday, Bumgarner went six innings and held the Dodgers to one hit - Mookie Betts' solo home run - in a 6-1 victory in Los Angeles. It was Bumgarner's first win since July 23, and Lovullo said that strong outing was a factor in his decision.
"I felt like, let's end the year on that and go into the offseason," he said.
In three years with Arizona, Bumgarner is 15-29 with a 4.98 ERA after going 119-92, 3.13 in San Francisco. He was the NLCS and World Series MVP in 2014.
"It's always a work in progress," Lovullo said. "This will be Act 2 of his career. It wasn't a good year; some adjustments need to be made. He will make those adjustments."
LAST HOME GAME
Sunday's attendance at Chase Field was 25,389, for a final season total of 1,605,199 - an average of 19,817 fans per game.
In 2019, the last season before COVID-19, the Diamondbacks drew 2,135,510, or an average of 26,364 per game.
The Diamondbacks finished 40-41 at home. They were 32-49 at Chase Field last season.
Diamondbacks: Off Monday before beginning a season-ending, eight-game road trip against the AL West champion Astros. RHPs Zach Davies (2-4, 4.03) and Houston's Lance McCullers (4-1, 2.38) pitch Tuesday, and Cy Young contenders Zac Gallen (12-3, 2.46) and Justin Verlander (17-4, 1.82) are scheduled to pitch Wednesday in Houston.