The result left the Merseyside club, who have won only one of their last 10 league matches at home, eighth on 52 points while Villa moved two places up to ninth on 48 points. Everton trail fourth-placed Chelsea by nine points with five games left to play.
Villa missed plenty of chances in an action-packed first half after Ollie Watkins fired them ahead in the 13th minute but grabbed the winner out of the blue when El Ghazi unleashed an 80th-minute thunderbolt into the top corner.
Their manager Dean Smith was pleased with the outcome and acknowledged Villa still had plenty to play for as they rekindled hopes of securing a place in Europe.
"It's easy for people to say we haven't got anything to play for but we want to finish as high as we can," Smith told the BBC. "We want to regain that momentum and consistency we had earlier in the season.
"It was a really good game of football and both teams tried to win. I think we deserved it on the balance of chances. Jordan Pickford was in really good form and denied Ollie Watkins time and time again."
Having spurned an early chance, Watkins gave Villa the lead as he robbed Mason Holgate of the ball and steered a low shot past Everton goalkeeper Pickford but sloppy defending cost the visitors shortly afterwards.
Dominic Calvert-Lewin levelled in the 19th minute when he nodded in a Lucas Digne corner at the far post and then missed a gilt-edged chance as Villa keeper Emiliano Martinez kept out the striker's close-range header.
Villa dominated the final 20 minutes of an absorbing first half as they missed one chance after another, with El Ghazi and Ross Barkley hitting the woodwork while Pickford denied Bertrand Traore twice in quick succession.
The pace fizzed out in the closing stages and just as the game seemed to be heading for a draw, El Ghazi picked his spot on the edge of the area and gave Pickford no chance with a delightful shot.
Everton manager Carlo Ancelotti was frustrated after his side's poor home league form continued.
"It is difficult to say why [Everton struggle at home]," said the Italian. "It is the same team who beat Arsenal seven days ago. We of course could do better at home for sure. It is unbelievable, the run we had away and the run we had at home.
"We made a lot of mistakes at the beginning -- no balance, no concentration. It was not a good evening."