St Helens (18) 40
Tries: Graham, Wellens, Flannery, Meli, Puletua, Gidley, Cunningham Goals: Foster 6
Castleford (4) 30
Tries: McGoldrick, Milner, Widders 2, Shenton, Hudson Goals: Westerman, Dixon 2
St Helens claimed a top two finish in Super League for the sixth year running as they booked home advantage for next week's play-off clash with Warrington.
But defeat ended Castleford's play-off hopes as they missed out on eighth place to Brian Noble's Crusaders.
In a dramatic finale, Cas came from 30-14 down with three tries in five minutes to tie the game up at 30-30.
But captain Keiron Cunningham's last-minute try was a fitting end to the last league game at Knowsley Road.
Coming on the back of Matt Gidley's try just two minutes earlier, it earned Saints the winning margin they needed to shade second place from Warrington, who they will now host at Knowsley Road next Friday night (2000 BST).
On an emotionally charged night, which marked the club's last league game at their 120-year-old home, Saints looked to have thrown it away by conceding three tries in the last 10 minutes, until they countered with a Houdini act.
Backed by a rousing reception, they got off to a blistering start, opening up Cas on the left with a blistering passage of play featuring seven fine passes.
Francis Meli and Ade Gardner burst down the left flank, the latter turning the ball back inside for the supporting Jonny Lomax to send Paul Wellens over after just two minutes.
They doubled their advantage six minutes later when James Graham hit a flat Cunningham pass to twist his way over, Jamie Foster goaling both tries to put Saints in the driving seat.
But Cas quickly proved that they were not there to make up the numbers on this historic night and Ryan McGoldrick went over from close range.
Saints were not helped by Kyle Eastmond picking up a knock for the third game in a row - although he stayed on to run the injury off.
And he was involved in Saints's third try, his grubber on the stroke of half-time being touched down by Chris Flannery.
But Cas grabbed an early second-half score five minutes after the restart after fine play from the impressive Rangi Chase opened the Saints defence up to send Adam Milner over, Joe Westerman's goal cutting the deficit to eight points.
Saints pepped up their game with the introduction of Sia Soliola, playing his first game since seriously damaging his knee ligaments in February. And they then pressed the Castleford line, Meli used a clever dummy and drive from acting half-back to drive over.
Moments later Puletua crashed over from close range, squeezing the ball over the line despite plenty of attention.
Foster converted both to give Saints a 30-10 lead, but no sooner had they settled into a comfort zone the Tigers came roaring back.
Dean Widders gathered his own kick to touch down on the right after Saints were unable to clear the ball.
Westerman missed the conversion attempt from wide out. But Castleford kept at it and hit Saints with three late tries Michael Shenton, Ryan Hudson and Widders racing in.
But Gidley struck two minutes from time and Cunningham wrote the fairytale ending by rumbling through a heap of bodies to touch down under the sticks.
St Helens coach Mick Potter:
"The game shifted momentum and both sides came up with some great plays. Castleford were on a roll for 10 minutes before that. But to snatch it towards the end was fairytale stuff.
"For Keiron to score the winning try underneath the posts was fitting. A lot will be written about Keiron - he is a legend of the modern game and just wants to do that little bit more.
"There was going to be no easy way through at the end and Keiron had four guys hanging off him at the end but he still managed to get it down."
Castleford Tigers coach Terry Matterson:
"We were not ready for the end of the season, but I am really proud of my guys. Although we have missed the top eight we can certainly hold our heads up high with the way we have overcome injuries this season.
"It took a while for us to get in the game and we looked pretty good and just after half-time, when we scored, I thought, 'here we go.'
"To come back from 30-10 to 30-all was tremendous. It took a couple of big plays from them at the end. That's the way it goes."
St Helens: Wellens, Foster, Gidley, Meli, Gardner, Lomax, Eastmond, Graham, Cunningham, Puletua, Flannery, Wilkin, Roby.
Replacements: Soliola, Hargreaves, Moore, Clough.
Castleford: McGoldrick, Dixon, Shenton, Ferres, Wainwright, Widders, Chase, Sargent, Hudson, Higgins, Jones, Snitch, Westerman.
Replacements: Huby, Milner, Walker, Arundel.
Referee: Ben Thaler (Wakefield).