The former Everton and Manchester United boss replaces Slaven Bilic, who was sacked on Monday with the Hammers in the relegation zone.
Moyes has been out of work since May, when he resigned as Sunderland manager after the club’s relegation to the Championship.
West Ham joint chairman David Sullivan said the 54-year-old Scot is “the right man to turn things around”.
He added: “We need somebody with experience, knowledge of the Premier League and the players in it, and we believe David can get the best out of the players.
“He is highly regarded and respected within the game and will bring fresh ideas, organisation and enthusiasm.
“He proved with Everton that he has great qualities and we feel that West Ham United is a club that will give David the platform to display those qualities again.”
Moyes’ first game in charge will be at Watford in the Premier League on 19 November.
He added: “I’ve managed five clubs since starting out nearly 20 years ago at Preston and then going to Everton. My period at Manchester United is well documented and I then did something I have always wanted to do by experiencing management abroad, with Real Sociedad.
“It’s only been the last job where I feel it wasn’t a good move and I didn’t enjoy the experience. So I’m hungry to make sure I get things right now.
“I don’t know any manager who hasn’t gone through negative periods, especially in the game today. I hope it gives me great strength and understanding of what is required.”