Ramos hit the grand slam off Baltimore starter Ubaldo Jimenez after the first three Rays (75-79) reached in the second inning. He finished 3-for-4 with four RBIs, getting the homer plus a double and a single.
Ramos nearly hit a second homer in the eighth inning. His shot to deep right was originally ruled a home run but overturned on a crew chief review and turned into a double.
Longoria's home run came in the third and let Tampa Bay tie a franchise record for homers in a season (216).
For Longoria, it was his 39th career homer versus Baltimore (74-81).
The home runs helped Alex Cobb (12-10) earn the victory after battling through six innings. He allowed three runs on nine hits and worked out of some jams but never gave up a big hit.
Jimenez (6-11), however, once again dealt with many problems.
He gave up six runs on eight hits in just three innings, and his ERA jumped to 6.81. Since this is the final year of his contract, it might have been his last start at Camden Yards as an Oriole.
Chris Davis hit the only home run for Baltimore, a solo shot in the fourth.
Lucas Duda added an RBI double for the Rays in the ninth to cap the scoring.
The Rays took an early 4-0 lead when Ramos crushed his grand slam 439 feet into the left field seat off Jimenez in the top of the second inning.
Baltimore cut the lead in half in the bottom of that inning thanks to an Austin Hays sacrifice fly and a wild pitch from Cobb.
Longoria led off the third with a homer and Adeiny Hechavarria's RBI single off Jimenez made it 6-2 later that inning.
Davis started the bottom of the fourth with a homer off Cobb, cutting the lead to 6-3. The Orioles had three singles but couldn't score in the previous inning as Manny Machado got thrown out at home by left fielder Corey Dickerson on Adam Jones' single.
Dickerson then gave Tampa Bay a 7-3 lead in the fifth with an RBI double off Miguel Castro.
NOTES: 2B Jonathan Schoop was selected as the Most Valuable Oriole in a vote by the local media members who cover the team. Schoop had a breakout season, made the All-Star team for the first time and had 105 RBIs heading into this game. ... CF Adam Jones' two hits moved him into fourth place on the Orioles' all-time hits list with 1,615, as he passed former OF Brady Anderson (1,614). ... C Wilson Ramos finished a triple shy of the cycle thanks to his homer, two doubles and a single. ... Tampa Bay pitchers struck out the side in the eighth and in the ninth. They recorded seven of the final nine outs via the strikeout.