The Hammers haul themselves off the foot of the table with a hard-fought but deserved win over the Terriers.