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 last 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 in a campaign which was dogged by injury.
"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."
The club's website described Carroll as their "marquee signing" who "represents a significant step for the club".
Liverpool sold Carroll at a significant loss, having bought him from Newcastle for a British record £35million on transfer deadline day in January 2011.
He never really fitted in at Anfield despite the best attempts of Kenny Dalglish, the manager who signed him.
Carroll scored 11 times in 58 appearances, but when Dalglish was replaced by Brendan Rodgers last summer it became apparent his chances of regular first-team football would be minimal.
A deal to loan him to West Ham was secured the penultimate day of the transfer window and despite some encouraging performances for the Londoners - which earned him an England recall - it was made clear to the striker Rodgers had not changed his view of him.
"Everybody at Liverpool FC would like to wish Andy all the best for the future," said a statement on liverpoolfc.com.
Carroll finished last season with a heel injury, which ruled him out of the national team's friendlies against the Republic of Ireland and Brazil and could keep him out of the new campaign, which the Hammers start at home to promoted Championship winners Cardiff.