When Grealish fired Villa ahead in the 84th minute it sparked joyous scenes on the touchline at the London Stadium.
But Andriy Yarmolenko's deflected effort levelled it up almost immediately meaning Villa had to endure a tense finale.
But when news came through the relief amongst the huddle of Villa players was immense.
Villa had been in the bottom three since February but a 1-0 win against Arsenal in midweek had lifted them above the trapdoor and put their destiny in their own hands.
They ended up with 35 points having picked up eight in their last four games of the season.
"It's been a hard season, a long one with the lockdown, but we never gave up," Villa's on-loan keeper Pepe Reina told BT Sport. "It seemed impossible two or three weeks ago."
"It's massive for the club, for everybody."
Villa survived an early scare when Michail Antonio fired wide for the hosts having latched on to a long ball.
But they were assured in defence and Grealish wasted a good chance just before the break, scuffing his shot straight at Lukasz Fabianski for an inviting position.
The second half was increasingly tense with West Ham substitutes Yarmolenko and Manuel Lanzini adding some threat to the home attack.
But Grealish, who could become a target for other clubs in the coming weeks, released the pressure when his powerful shot from just inside the area flew past Fabianski to give him his first goal since January.
Villa's expressions turned from joy to horror a minute later, though, when Yarmolenko cut in and fired a left-footed shot that struck a knee and lopped agonisingly over Reina.
"We were aware of the other scores," manager Smith said. "When Jack scores we though that was it, we're safe, but they got a deflected goal. We weren't great today in all honesty but we defended resolutely.
"It's magnificent achievement for everybody."