The 24-year-old finalised the deal, which is understood to have cost the Hammers £15.5million plus a further £2million in add-ons, after successfully undergoing a medical on Tuesday night.
Despite the lengthy contract, West Ham have the option to add a further two years if they wish.
Carroll spent last season on loan at the Boleyn Ground, scoring seven times in 24 league appearances despite an injury-hit a campaign.
"It's fantastic for me to be here. I really enjoyed last season, and that shows in me coming back and making it permanent," he told whufc.com.
"The fans, the lads and the club itself has been great to me, and what I wanted to do was come back here and play football.
"Since the end of the season I've had a lot of time to think, I've missed it and that's why I'm back."