First-half goals from Sam Winnall and Josh Scowen cancelled out Sean Morrison’s early header to give the Tykes a two-goal advantage over the home side at the break.
Strikes from Peter Whittingham and Anthony Pilkington in the space of 10 minutes looked to have rescued Cardiff.
But Williams’ late winner snatched the three points to give the Tykes just their fourth win in 15 games.
Cardiff got off to the perfect start when an unmarked Morrison headed home Whittingham’s corner with ease in the third minute.
Such poor defending was surprising to see from a Barnsley defence who had previously kept three clean sheets in their last four away games.
The Tykes equalised in the 19th minute through top scorer Winnall after Conor Hourihane’s cross found the Barnsley striker whose header looped over a helpless Ben Amos.
Hourihane leads the Championship charts with nine assists this season and put on another fine performance.
Cardiff should have retaken the lead when Junior Hoilett found himself one-on-one with Adam Davies, but the Canada international skewed his shot wide.
Hoilett was involved again shortly after as he played in Kenneth Zohore whose shot was saved by Davies. The resulting corner saw the Barnsley goalkeeper save Bruno Magna’s goal-bound header.
Neil Warnock’s men would rue those chances as Barnsley went in front in the 32nd minute following a clinical counter-attacking move.
A Cardiff corner was cleared and Ryan Kent sprinted down the left wing to cross to Sam Morsy whose superb lay-off allowed Winnall to slot the ball past Amos for his ninth goal of the season.
Barnsley extended their lead two minutes from the break thanks to a second Amos goalkeeping error in as many games.
Amos rushed off his line to try and beat Kent to Hourihane’s defence-splitting pass, but the Liverpool loanee won the race and squared the ball to Scowen who had a simple tap-in.
The former Bolton keeper was also at fault for Wolves’ goal in the Bluebirds’ 2-1 win on Tuesday.
Cardiff started the second-half with intent as Hoilett hit the post from close-range.
The home side pulled a goal back with 11 minutes to go through Whittingham after his deflected shot beat Davies.
The dramatic equaliser came in the 89th minute after Pilkington poked the ball home following Morrison’s knock-down from a corner.
But two Barnsley substitutes combined well as Marley Watkins’ cut-back was met by Williams who scored with his first touch in injury-time.