Jennings used long strides to get to third, and after B.J. Upton grounded out to first, Longoria lined a single to center to end the game.
"It was a really entertaining, well-played game. To win that is significant," Rays manager Joe Maddon said.
But the 11th-inning rally saved Tampa Bay, which has won three in a row -- including the opener of this three-game series on Friday -- and six of seven overall.
The Rays climbed within 4 1/2 games of the Red Sox for the American League wild card lead. The Red Sox have lost eight of 10.
"Hopefully we'll be able to take some of these bumps and bruises and be able to learn from there," Red Sox closer Jonathan Papelbon said.
Farnsworth entered in the ninth to secure the save and retired the first batter, but Saltalamacchia followed by lining an 0-2 hanging breaking ball just over the wall in right field.
Ellsbury followed with a blast a little farther into the right field stands, making it a 5-5 game.
Ramos recorded the first two outs of the 10th before Brandon Gomes (2-1) took over. Gomes finished the inning and pitched a 1-2-3 top of the 11th.
The Rays loaded the bases with nobody out in the first inning, but got only one run out of the situation. Ben Zobrist grounded into a fielder's choice before Johnny Damon looked at a third strike and Matt Joyce flied out.
The teams traded run-scoring groundouts in the second, and Kotchman's sacrifice fly in the third put the Rays up 3-1.
Gonzalez's two-run blast off Tampa Bay starter Jeremy Hellickson in the fifth tied the game, but only briefly. Kotchman's solo shot to right in the home half made it 4-3.
Longoria had a sacrifice fly in the sixth.
Hellickson pitched six innings, allowing three runs on five hits and three walks...Boston starter Kyle Weiland threw four innings, giving up three runs on six hits and three walks...Gonzalez's homer was his 25th of the season, the fifth consecutive year he's reached that mark.