For a long time it looked like relegation-threatened West Ham would be able to hold off.
In the 79th minute, the home team's Mateus Fernandes created the best chance of the match. But Arsenal's David Raya made one of the most important saves of his career so far.
Four minutes later, Martin Ødegaard played to Trossard, whose shot broke the deadlock.
West Ham then equalized from a corner, but the VAR room ruled out the goal after Raya was treated too roughly.
The victory means Arsenal now lead the league by five points, ahead of second-placed Manchester City, who have a game in hand. West Ham, meanwhile, remain in 18th place with just two games left to play.