Cobb (9-8) issued only four hits and a walk while striking out five over seven innings for the Rays, who clinched their three-game series and pulled to within 1 1/2 games of the Yankees and the Baltimore Orioles atop the American League East.
Longoria's blast in the third inning erased a 2-1 deficit and put Tampa Bay ahead for good.
Desmond Jennings and B.J. Upton added back-to-back solo shots in the fifth.
Freddie Garcia (7-6) was touched for five runs on five hits over 5 1/3 innings as New York lost for the 11th time in 12 games at Tampa Bay.
Robinson Cano belted a two-run homer for the Yankees. who surrendered sole possession of first place in the AL East after holding it for 84 days.