The win keeps New York four games back of Detroit for the American League's second Wild Card.
Headley, who also delivered the game-winning hit in his first game with the Yankees, raised his right fist as he raced down the first-base line. He was doused with an ice bucket shower at home plate, having helped the Yankees take two of three games from Boston to open a make-or-break homestand.
"I actually knew everybody's name this time," Headley said. "Every one is fun. There's not a whole lot of better feelings in sports and in baseball than getting a walk-off hit. Obviously the circumstances that we're in right now, we've got to win every game that we play. That's our mentality. Big, big win."
Both homers came off hanging splitters from closer Koji Uehara and could have provided the spark that the Yankees need to get their bats in motion for a September push, particularly with the slumping Teixeira, who hadn't homered since Aug. 17.
"It's been an up-and-down season for all of us," Teixeira said. "When you can win a game like that, win a series in that fashion, it just doesn't happen very much. You don't hit two home runs off one of the best closers in baseball very much."
Headley said that Teixeira's at-bat took the pressure off, allowing him to swing for the back pages.
"You're playing with house money at that point and you can be aggressive and try to get a good pitch to hit, and that's what I was able to do," Headley said.
Adam Warren picked up the victory in relief, having hurled a scoreless top of the ninth in which he pitched around his own error on which he fumbled a sacrifice bunt. With manager Joe Girardi mixing and matching in playoff mode, five relievers tossed 4 2/3 innings of scoreless ball.
"I think games like this, when we're called on to give a lot of innings, we try to take the team on our back," Warren said. "We say we're going to keep the Red Sox there and allow the offense to come back. And that's what we did."
Chris Capuano started for New York and allowed four runs in 4 1/3 innings, serving up three homers -- two to David Ortiz, who cleared the fences in both of his first at-bats of the night.
"[The win] was absolutely huge, especially the way the game started," Capuano said. "We were kind of in a hole there early and for them to really grind it out, keep it close and come through there at the end -- every game is huge for us right now."
Brock Holt also homered off Capuano to give Boston the lead in the fifth, and Girardi said that he had no intention of allowing Capuano to try his luck against Big Papi a third time. Left-hander Rich Hill entered and struck out Ortiz, and Boston managed just one hit the rest of the way.
"When you have September baseball, you can mix and match more," Girardi said. "Each guy did his job and did it extremely well."
The Yankees chipped back with three third-inning runs off Brandon Workman, a surge highlighted by Derek Jeter's two-run double -- a well-struck drive that flew over Mookie Betts' head in center field and marked the captain's third extra-base hit since Aug. 3.
Ichiro Suzuki and Jacoby Ellsbury scored on Jeter's drive, setting up Carlos Beltran to tie the game with a single to right field. The hit was also Jeter's 540th career double, tying Joe Medwick and boyhood idol Dave Winfield for 32nd on the all-time list.
"I've talked about this guy, he's been big in the months of September and October," Girardi said. "He understands what it's all about. I thought he swung the bat pretty good last night and the night before, too. But this is a guy we need to be productive as well."
Workman completed six innings, scattering five hits with two walks and five strikeouts. As in the first two games of the series, a baserunning blunder short-circuited a Yankees rally: this one in the seventh inning, as pinch-runner Antoan Richardson was doubled off first base on a line drive to center field.
The ninth-inning blasts relegated Richardson's inauspicious debut to a footnote while teasing with thoughts of what could be. Headley recalled watching the Yankees from afar, noting how they always seemed to be "jumping up and down on the field a lot."
"You just kind of learned to expect it," Headley said. "I'm sure people around here got spoiled with it a little bit."
Teixeira lived it first-hand in 2009, and for the first time in a while, that didn't seem like so long ago -- a time when the Yankees regularly conjured this kind of late thunder on the path to October.
"Two home runs in the ninth, it used to happen a lot more," Teixeira said. "We'd like for it to happen more. We know home runs have been down for our team, but we're not going to go out there trying to hit home runs. But it's a lot of fun when you do."