Whitehead, who has played 59 Blue Square Bet Premier matches for the Hatters, joins the Hammers for an undisclosed fee with an option of a third year.
The attacking midfielder, who made his first-team debut in 2011 at 17 years of age, will join up with Sam Allardyce's men for pre-season training on July 1.
Whitehead said: "I'm very excited to have joined West Ham and I can't wait to get out there and play.
"It's obviously a big step up, but I've got to take the opportunity and grasp it.
"I was on trial here at the end of last season and all the lads helped me fit in well during the two weeks of training I did.
"I've got a bit of experience from playing for the first team at Stockport - I'll probably be with the U21s here at the start and then try to push towards the first team."
Allardyce added: "He was recommended by Didi Hamann - who managed Stockport - as he felt he was an emerging talent that needed nurturing.
"I asked their current manager Ian Bogie about his talents and he allowed us to take a look at him last season and like every young player he's got talent.
"He's very capable of handling the ball and he's able to create with his passing.
"He's very slight, which we'll work on, but the question is can we create that potential into a Premier League player? It is something we'll find over the coming years through our Academy system and we'll see if he can join the long list of players that have come through."