The versatile Republic of Ireland's current deal was due to expire at the end of a campaign in which he has established himself at left-back for Sam Allardyce's side
The Republic of Ireland international was acquired by the Hammers on a free transfer after his contract with Bolton expired in 2011.
He has cemented his place at left-back upon the club's return to the Premier League this season but was out of contract at the end of the current campaign.
"I am delighted," O'Brien told the club's official website. "It's for three-and-a-half years so hopefully I'll have plenty more good times with the club and that's what I'm looking forward to doing now.
"I definitely dreamed of what has happened since I joined West Ham. When I got released by Bolton, I had not played for about two-and-a-half years in the Premier League or really any games in that length of time, so since I have come here things have worked out well from the start.
"A manager that I knew [Sam Allardyce] got the job at this club and invited me down to training and having that six-week trial seems a long time ago now. Thankfully for me, that went well.
"Last year, it went great. To be back playing some games was fantastic for me and, in the end, we got the job done and got promoted. This year we have started really well and it's been good. Our aim was to finish in the top half and we're on course to do that."
The 26-year-old made his first-team debut for Bolton in 2004 while Allardyce was managing the club, and made 70 appearances for the Trotters.
He managed only 12 games between 2008 and 2011 after suffering a serious knee injury, but featured regularly as the Hammers gained promotion to the Premier League in the 2011-12 season.