Wales's Shane Williams tries to find a way through Samoa's defence during the Rugby World Cup game. Photograph: David Davies/PA

Having lost in beautiful style to South Africa, Wales won pig-ugly against Samoa. They scrambled their way to 17 points with a try they nearly failed to finish and a set of penalties that came from rare indiscipline on the Samoan side.

That said, they would settle for hideous any day. The result, as their head coach, Warren Gatland, confirmed afterwards, was what counted. The performance did not. He paid tribute instead to the character and the fitness of his team.

Samoa refused to concede that their chances of qualifying for the last eight had disappeared with defeat. But they too needed a result before anything else. This was the one they wanted and even if they had grumbled about having to play again so soon after beating Namibia, they were always going to be at full throttle here. History, after all, said they should win this.

Wales's record against Samoa at the World Cup spoke only of defeat, in 1991 and 1999. And this Samoan outfit, unlike teams of the past, was anything but hastily assembled. This was a crafted formation, carefully prepared to feed their ferocious runners with a regular supply of ball. That Wales prevented Alesana Tuilagi from running over Rhys Priestland more than four times is a tribute to every one of the 142 tackles they made in the match.

Their victory was not entirely based on defence. Jamie Roberts had another storming game in the centre, both as battering ram – his changes of angle have reignited his ability to make ground through heavy traffic – and as a deft passer of the ball across his fingertips. His influence would have been even greater if Wales had not lost control of the breakdown in the second and third quarters. It was at this time that the game hung in the balance, when somebody had to make a stand.

Sam Warburton, Wales's captain at 22, was their symbol of defiance. He had another extraordinary game, tackling himself to a standstill, bouncing back to his feet and forcing at least three turnovers. His task was made even more demanding by the early exit of Dan Lydiate, his more invisible back-row partner, who turned an ankle.

The No6's replacement, Andy Powell, is never anything but highly visible, but there is something reassuring about the efficiency of Lydiate and Wales were forced into a scrambled defence sooner than they are when he is there to stop opponents at source. Without him Samoa had more room for the scrum-half Kahn Fotuali'i to snipe and feed his forwards.

Samoa had more chances to score tries than Wales but they managed only one, on the stroke of half-time, when the prop Anthony Perenise forced his way over from short range for a 10-6 lead.

They continued to govern the third quarter, when a whole series of penalties were awarded against Wales: for holding on, or for going off their feet at the ruck. They seemed to be feeling the pressure and it began to show. Priestland kicked out on the full, a set move across the three-quarter line became a shambles of misunderstanding, Powell dropped a pass from a ruck. He juggled a pass immediately afterwards and a groan went up. But he clung on for Roberts to combine with Leigh Halfpenny, who had replaced James Hook at half-time, the full-back having damaged a shoulder. Halfpenny had a fine half, setting up the position from which Priestland landed his second penalty to put Wales into the lead, 12-9.

And the new full-back started the counter-attack that led to the Wales try. He caught a clearance, ducked under two tackles and darted away from a third. He passed to the centre Jonathan Davies, who raced away with only one pass to make. He paused and paused again and then tried an overhead flip to Halfpenny, only to miss him. Luckily Shane Williams was backing up and he went over for his 55th Wales try.

There was still much work to do. Tuilagi was rampaging at Priestland and every ruck involved a brutal, entirely legal assault from the Samoan forwardsThe pressure was right back on Wales. Davies the try-maker tried to relieve the pressure with a long kick, but the ball had come back into the 22 and he belted it out on the full.

Samoa set up a series of short-range drives, just like the ones that had led to their try. This was for the draw, which would have made the fight for the runners-up spot in Pool D very interesting. Suddenly Toby Faletau, who had another strong game at No8 for Wales, appeared with the ball on his own line and won the put-in at the scrum.

By now, Gethin Jenkins was on, playing Test rugby for the first time since November. He brought his tackling and his solidity. The last minutes were survived and Wales were celebrating a result, quite happy to put the performance in the bin.