"I am delighted to announce that our manager Sam Allardyce has signed a new two-year contract," West Ham co-owner David Gold announced on Twitter.
Allardyce, 58, had always said he would only sign a new contract once the club were certain of survival in the Premier League.
West Ham are now eight points above the relegation zone with only two games remaining.
Allardyce's contract was due to expire at the end of the season.
The former Bolton and Newcastle manager said on Friday that he would be signing a new contract "very shortly".
"I never thought there would be any problem," Allardyce said.
The Hammers recently learned they will be anchor tenants for the Olympic Stadium, and Allardyce said he would be like to be in charge of the team when they move into their new home in three seasons' time.
"If I can last that long, I'd like to be here," he said. "The sack is always around the corner, that's how it is these days."
Spare a thought for Blackburn fans who must rue the day that the Venky family dispensed with Allardyce after buying the Lancashire Club. If he had remained at Ewood there is no doubt in most people's minds they would still be in the Premier League!