Samoa on Sunday edged the first half 10-6 but Wales stormed back after the break as a Welsh-record eighth World Cup try for Williams earned their first ever win over Samoa in the tournament after major upsets in 1991 and 1999.
The win, following Wales’s 16-17 opening loss to defending champions South Africa, puts them in the box seat to qualify second from Pool D with a likely quarter-final against Six Nations rivals Ireland.
“We knew it was going to be a tough battle and it was,” said Wales skipper Sam Warburton.
“If we lost the chances were that we were going home, so there was a lot of pressure on the boys.”
Samoa skipper Mahonri Schwalger said: “We still have a chance in this World Cup. Credit to the Welsh, they played quite well. We had a few chances to score some points, but we didn’t take them.”
In bright and breezy conditions at a packed Waikato Stadium, James Hook’s early penalty from the halfway line went narrowly wide and Samoa passed up a golden chance when Sailosi Tagicakibau spilled a low pass just metres from the tryline.
Wales suffered a blow when flanker Danny Lydiate limped off in just the 11th minute to be replaced by Andy Powell. But Hook was gifted an easy three points when Maurie Faasavalu high-tackled Rhys Priestland near the Samoan 22m line.
Jamie Roberts crossed on 14 minutes but was brought back for a forward pass and Rhys Priestland missed his third drop-goal of the World Cup, before Samoa’s Paul Williams levelled the scores with a penalty from the Welsh 10m line.
Samoa’s forwards disintegrated under a powerful Welsh scrum to give Hook another penalty to make it 6-3, but they made amends with a punishing, 18-phase assault on the tryline which ended only when Faasavalu illegally grounded the ball.
Samoa cartwheeled a scrum on the halfway line as their forwards took control and they dealt Wales an enormous hit when another crunching attack culminated in Perenise’s try — and Williams’ conversion — for a 10-6 half-time lead.
Welsh fullback Hook was replaced by Leigh Halfpenny at half-time after a shoulder injury and they reduced the margin to 10-9 when Priestland’s penalty fortuitously bounced off the crossbar and over.
The Wales backs were suddenly in the game and centre Jonathan Davies had the tryline at his mercy when a long cross-field kick bounced off his legs, but Priestland then kicked them 12-10 ahead after a ruck infringement.
And Wales were again rewarded for their greater endeavour when Halfpenny broke down the left and fed Davies, whose panicky one-handed pass was gathered off the floor by Williams for his 55th Test try, 13 minutes from the end.
Samoa’s forwards massed for one final assault but the attempt was undone when number eight George Stowers lost control of the ball at the tryline.
Wales were stunned 13-16 by debutants Samoa in the 1991 World Cup, and the Pacific islanders returned to Cardiff to repeat the feat eight years later.
Rugby power Wales have only reached three quarter-finals in three editions of the rugby showpiece while Samoa, an island nation of just 180,000, have twice reached the knock-outs in 1991 and 1995.
Wales face African minnows Namibia in New Plymouth on September 26 while Samoa’s next Pool D opponents are Fiji in Auckland on September 25.