History was made at Anfield as Liverpool established a Premier League record of 21 consecutive home wins and equalled Manchester City’s run of 18 successive top-flight victories. The landmark was created with great difficulty and greater belief. West Ham surpassed expectations in making Jürgen Klopp’s leaders look vulnerable but, like every other domestic visitor this season, they departed Anfield in the knowledge their best was not enough.

Liverpool moved to within 12 points of the slice of history that matters most – their first league title for 30 years – thanks to many of the characteristics that have come to define their remarkable campaign.

Trent Alexander-Arnold made the breakthrough for Georginio Wijnaldum and the late winner for Sadio Mané as he moved to 12 assists for the second season running. Andy Robertson continued the theme of Liverpool’s devastating full-backs by setting up Mohamed Salah for the second, converted thanks to a dreadful mistake by West Ham’s goalkeeper Lukasz Fabianski.

At that point the visitors led through the substitute Pablo Fornals and Liverpool, sluggish for long spells, were in trouble. But they were never subdued. Klopp’s players rarely are these days. Roared on by an equally convinced Anfield crowd Liverpool found a way once more.

David Moyes has never won in 16 visits to Anfield and, even after Fornals had swept West Ham into a merited lead early in the second half, he refused to get carried away with the celebrations of his staff on the bench. Experience has taught him not to, and so have West Ham’s problems in goal this season.

Fabianski made several fine stops but was at fault for the first and the crucial second goal, letting Salah’s low shot squirm between his legs and over the line, as West Ham remained ensconced in the relegation zone without a win in seven league games.

Their problems are not restricted to the field of play either, with kick-off the signal for a mass protest towards the club’s board by the travelling supporters.

The black balloons that formed part of the West Ham protest were still being popped when Liverpool took the lead.

When Roberto Firmino saw a shot blocked inside the West Ham area the threat to Fabianski’s goal appeared over. Or so the visitors thought. The ball spun towards the corner flag where Alexander-Arnold whipped over a trademark peach of a cross for Wijnaldum to head beyond Fabianski.

Robert Snodgrass and Issa Diop reacted too late to the cross and the midfielder’s darting run but the Poland keeper was also culpable. Wijnaldum’s header was hardly bullet-like but found its target via Fabianski’s weak forearm.

Lukasz Fabianski lets Mohamed Salah’s shot slip through his hands for Liverpool’s equaliser. Photograph: Phil Noble/Reuters

It was the worst possible start to what had always threatened to be a long night for Moyes’s team but West Ham’s response could not be faulted. They were level within three minutes through the first of several dangerous set pieces, a fine header from Diop on the end of a Snodgrass corner with Alisson being caught by surprise at his near post. It was the first Premier League goal Liverpool had conceded on home soil since 4 December and prompted an outbreak of “We’re gonna win the league” from the visiting fans. Laughter emerged from the rest of the stadium.

Fabianski produced a fine save to deny Salah, Virgil van Dijk struck the crossbar with a header from an Alexander-Arnold corner and the right-back went close with a low drive from Firmino’s lay-off. Despite the chances Liverpool struggled to find their rhythm and Jordan Henderson’s absence was sorely felt in central midfield.

The departure of Tomas Soucek early in the second half through injury was significant, as the January recruit had been highly effective in the West Ham midfield, but his replacement made a greater impression.

Fornals had been on the pitch eight minutes when he stunned Anfield by sweeping the visitors ahead. Declan Rice delivered a low cross from the right, Joe Gomez was slow to react and the Spanish midfielder steered a first-time shot wide of Alisson’s despairing dive, into the Brazilian’s bottom right-hand corner.

As always, Klopp’s players refused to be derailed by a deficit or a jaded display. Alexander-Arnold stung Fabianski’s fingers with a soaring drive before Liverpool levelled courtesy of a shocking error from the West Ham keeper.

It followed a fine move, started by Alex Oxlade-Chamberlain’s crossfield ball to Robertson and his darting run inside Snodgrass to set up Salah.

The forward’s shot was low and straight at Fabianski but squeezed through the keeper’s hands and trickled over the line. Cue despair for West Ham and greater relief for the champions-elect.

Liverpool pressed on for the record-breaking win and, somewhat inevitably, it arrived late in the day.

Alexander-Arnold was quickest to react when Gomez’s shot deflected off Noble and sailed into the West Ham area. As Fabianski advanced the full-back had the presence of mind to lift the ball over his head and there was Mané, unmarked, to tap home the winner.

There was still time for another West Ham substitute, Jarrod Bowen, to race through on Liverpool’s goal – he tried to chip the advancing goalkeeper – but Alisson made a vital, telling save to secure another three points. Only 12 more, at the very most, to go.