The 25-year-old played eight games for the Raiders in 2016, who lost in last weekend’s play-offs last four.
He returns for a second spell in Super League, having played 10 games for Catalans Dragons in 2014.
“Sam will bring a wealth of experience and a good kicking game,” said Wildcats head coach Chris Chester.
“He is a great signing for the club, a big coup and a number of Super League clubs were chasing his signature.”
Williams discussed Chester’s methods with Raiders teammate Josh Hodgson, who played under the Trinity coach at former club Hull KR.
“I have heard good things from Josh on his coaching and I am now excited to be a part of it and am looking forward to what the future holds,” he added.