Trailing 6-2 after being held down by starter Cole Irvin, the Rangers took advantage of the A's struggling bullpen. DJ Peters hit a two-run home run and Yohel Pozo added an RBI double off Sergio Romo before Heim's homer on an 0-2 pitch from Andrew Chafin (1-3). The ball travelled an estimated 447 feet.
"I don't think I've ever hit a ball that far," said Heim, who spent last season in Oakland before being traded to Texas in the offseason as part of a deal that included Elvis Andrus. "As a young team and young guys, it shows you can win a ballgame at any time. It just proves to these guys to stay on course and keep fighting until the last out."
It's the sixth time in 76 games this season that the Rangers have come back to win after trailing going into the eighth.
"I just love how the guys rallied around each other," manager Chris Woodward said. "We fell behind and it's easy to kind of write that one off. It's a playoff-contending team, they're clawing and scratching to make the playoffs. In that eighth inning guys had quality at-bats one after the other. Huge hits. One of our best wins, if not the best, of the year."
Matt Olson, Starling Marte and Mark Canha homered for the A's.
Oakland, which entered the day one game behind the Yankees for the second AL wild card, has eight blown saves in the last 19 games.
"We have a lot of confidence in Romo and Chafin finishing off that game. It just didn't happen," A's manager Bob Melvin said. "It's hard to give up five in the eighth inning when you feel like you have the game in control."
Texas hadn't done much before the eighth inning. Nick Solak had an RBI double and scored on Yozo's single in the fourth but the Rangers stranded two on in the second and had only one batter reach base from the fifth through the seventh.
Heim's home run was the first by the Rangers in five games.
"I'm just glad I could put together a good at-bat for the team," Heim said. "I got that one pretty good. When you hit a ball that good you can admire it a little bit."
Leody Tavares connected off Yusmeiro Petit in the ninth.
Brett Martin (4-4) retired three batters to win. Joe Barlow pitched the ninth for his fifth save.
The A's loss spoiled a strong start by Irvin, who had eight strikeouts in seven crisp innings. The left-hander allowed six hits and two runs with one walk.
"Today was a good day for me," Irvin said. "A heartbreaking loss, though."
Olson hit his 33rd home run in the third inning -- and got his 500th career hit at the same time. Canha added a two-run shot in the fourth. Marte homered in the first.
All three home runs came off emergency starter Wes Benjamin, who got the call after scheduled starter Kolby Allard was a late scratch because of an illness. Benjamin allowed six hits and six runs in 3 1/3 innings.
SORRY ABOUT THAT
Nathaniel Lowe was in the on-deck circle in the eighth inning when he got hit in the right knee by a foul ball from Peters, who appeared to be trying to check his swing. Lowe chuckled after getting hit and stayed in the game, then drew a walk and scored on Pozo's double.
BASSITT BACK ON A MOUND
Injured A's ace Chris Bassitt threw 30 pitches in his return to the mound for the first time since getting hit in the head by a 100 mph line drive in mid-August.
"It was just more about getting him on a mound, getting comfortable throwing again," Melvin said. "Obviously we've got to make sure everything's healed in there and the doctors have to clear him, but after that, with him it's going to be all systems go."
Rangers: Allard could possibly pitch out of the bullpen Sunday, according to Woodward. ... OF Willie Calhoun is expected to begin a rehab assignment with Triple-A Round Rock on Monday. Calhoun has been out since suffering a broken left arm in late June. RHP Drew Anderson (COVID-19 IL) and IF/OF Brock Holt (COVID-19 IL) are also on rehab assignments with Round Rock.
Athletics: 3B Matt Chapman came out for unknown reasons in the fifth inning. ... RHP Mike Fiers (sprained right elbow) will pitch in a rehab game with Triple-A Las Vegas next week. Fiers hasn't pitched in the majors since May 6.
RHP James Kaprielian (7-4, 3.81 ERA) makes his 20th start of the season Sunday, the most by an A's rookie since 2017. Kaprielian is unbeaten in six starts since returning from a right shoulder impingement injury that landed him on the injured list. Rangers LHP Taylor Hearns (5-4, 3.95) is winless in five career games against Oakland.