Mark Noble put the Hammers in front from the penalty spot on 15 minutes after Aaron Rowe, on his debut, had fouled Manuel Lanzini.
Juninho Bacuna headed in Aaron Mooy's corner to level the scores before Karlan Grant put the Terriers ahead on the half-hour.
Grant extended the lead after the break with a superb strike before Angelo Ogbonna pulled a goal back, heading in Aaron Cresswell's corner.
Chicharito made it 3-3 with six minutes to play before winning the match with another header in the first additional minute.
West Ham move to 42 points and ninth, while Huddersfield remain bottom on 14 points.