The Giants were poised to give Matt Cain a victory and send Patrick Corbin to his first loss in 10 decisions before Goldschmidt lined a 2-0 pitch from Jeremy Affeldt over the right field fence for his 15th home run of the season. Goldschmidt had twice grounded into double plays and struck out in his first three at bats.
Cain and Corbin were both scoreless until Gregor Blanco's two-out single brought Pablo Sandoval home from third in the seventh.
The Diamondbacks remain the only team in the majors not to be shut out this season.
Brad Ziegler (3-1) pitched two-thirds of an inning in relief of Corbin to get the victory. Affeldt (1-2) took the loss. Heath Bell pitched a scoreless ninth for his 10th save as the Diamondbacks improved to 12-0 in games Corbin starts.
Arizona stayed 2 1/2 games ahead of Colorado in the NL West. San Francisco dropped to 3 1/2 back.
Cain went seven innings plus a batter, allowing a run on four hits with three walks. Corbin gave up a run on seven hits in 7 1-3 innings, striking out four with no walks.
Cain left after A.J. Pollock beat out an infield grounder to third for a base hit. With Affeldt on in relief, Pollock advanced to second on pinch hitter Wil Nieves' groundout to second. Affeldt got Gerardo Parra looking, then Willie Bloomquist walked to bring up Goldschmidt, who was coming off a road trip in which he hit two grand slams. The big Arizona first baseman increased his NL-leading RBI total to 57, acknowledging the crowds cheers with a wave from the dugout.
Cain settled down after a 31-pitch first inning, when he walked three and gave up a single but still held Arizona scoreless.
Goldschmidt grounded into a double play, leaving a runner at third with two outs. Cain walked the next two batters to load the bases but got Martin Prado on a broken-bat groundout to end the inning.
Didi Gregorius led off the Diamondbacks' fifth with a double and, with a head-first slide, barely beat Gregor Blanco's throw on A.J. Pollock's fly out to center to put a runner on third with one out. But Gregorius tried to score on Corbin's weak grounder to first and was easily thrown out by Brandon Belt.
Corbin retired 12 in a row before Sandoval's broken-bat single with one out in the seventh. Belt singled sharply to right to put runners at first and second. Sandoval took third when pinch hitter Joaquin Arias flew out to right, then Blanco's first-pitch single to center put San Francisco up 1-0.
First base umpire Jerry Layne, the crew chief, aggravated a rib injury and left the field near the start of the Arizona half of the third inning. The crew went with three umpires the rest of the game.
Notes: Corbin was trying to be just the third pitcher in 25 years to start 10-0. Roger Clemens (11 in 1997) and Aaron Small (10 in 2005) were the others. ... Goldschmidt sent a pair of shots outside the right- and left-field foul poles before grounding into his first-inning double play in the first. ... San Francisco has won eight of its last nine at Chase Field. ... The temperature was 108 degrees outside, 83 inside Chase Field at first pitch. ... Madison Bumgarner (4-4, 3.46 ERA) starts for the Giants and Trevor Cahill (3-6, 3.27) goes for Arizona on Saturday night. ... San Francisco has lost seven of 10.