Trinity scored back-to-back tries on eight and 10 minutes to take a shock 12-0 lead and were 14 points clear early in the second half.
Warrington hit back and Trinity were forced to defend desperately in the final 10 minutes.
They seemed to have done enough, but a knock-on by David Fifita gave Warrington one last chance and they made it count as Lineham forced his way over at the corner from Stefan Ratchford’s long pass.
That waas tough on Trinity, who were tenacious in defence and clinical with their finishing.
Ashley Gibson raced through a gap from Kyle Wood’s kick for the opener, after he had gathered a grubber from Liam Finn.
Then in the set from the restart Dean Hadley turned the ball inside to Jacob Miller and he dashed into space before sending the supporting Mason Caton-Brown over.
Finn converted both, but Trinity had an escape soon afterwards when Declan Patton’s kick was knocked back by Lineham and Ryan Atkins touched down against his former club.
Referee Ben Thaler thought it was a try, but asked video assistant Robert Hicks to check and he spotted the centre had come from an offside position.
There was no denying Atkins at the start of the second quarter when he accepted Ratchford’s pass deep in his own half and powered clear, shoving off Max Jowitt - the Wakefield full-back making his first appearance of the season - before touching down between the posts.
Patton converted, but Finn landed a penalty goal - after Jack Hughes picked up a loose ball in an offside position - just before the interval.
In the final seconds of the half Miller made a clean break, but Ratchford stood his ground to pull off a try-saving tackle and then Caton-Brown’s pass as the hooter sounded could not pick out Ben Jones-Bishop.
Wakefield extended their advantage six minutes into the second half when Miller’s terrific long pass gave Tom Johnstone a walk-in at the corner and Finn’s goal made it 20-6.
Warrington got back in the game with 23 minutes left through captain Chris Hill, who barged over between the posts to make Patton’s kick a formality.
Andre Savelio was held up over the Trinity line and desperate defence dragged Joe Philbin down just short but Trinity cracked for the third time with 10 minutes left when Kevin Brown dummied over from close range and Patton’s conversion made it a two-point ball game.