The Toffees entered the game having lost six of their last seven Premier League games, with pressure piling on manager Rafa Benitez.
Richarlison thought he had opened the scoring just before half-time, but VAR ruled him offside when heading the ball past Aaron Ramsdale.
And things went from bad to worse when Arsenal playmaker Martin Odegaard broke the deadlock only one minute later.
Kieran Tierney delivered an excellent cross to the near post and the Norwegian was able to finish fortuitously past Jordan Pickford.
Richarlison was denied by VAR once again shortly after half-time in another tight offside call.
But it was third time lucky for the Brazilian when his 79th minute header lobbed Ramsdale.
The game looked destined for a draw, but with the Goodison crowd getting behind their team, it was Gray who proved to be the hero.
After some excellent work from substitute Andre Gomes, the summer signing cut inside on his right foot and moved past Ben White and Takehiro Tomiyasu, before unleashing a rasping strike off the inside of the far post and into the net.
It is a massive win for Everton and one which could prove a turning point for their 2021/22 campaign.
Benitez's side are now eight points clear of the drop zone.