The Steelmen will be playing in qualifiers for next season's Europa Conference League following a match which saw Joe Efford shoot the hosts in front early on before Hearts quickly equalised with a strike by Josh Ginnelly.
But Lamie – who has signed a pre-contract with Dundee – slammed the ball home in the 56th minute after a corner had only been partially cleared by Hearts’ Peter Haring.
Motherwell, who had gone from Boxing Day until April without winning a league game, recorded back-to-back wins for the first time since December and moved up to fourth place with just Saturday’s trip to Celtic left.
“It’s a brilliant feeling to qualify for Europe,” said Motherwell manager Graham Alexander.
“No-one saw that a month ago, did they? I am so pleased for everybody connected to our club and certainly the players and staff who have put so much into it.
“We’ve had to put the tin hats on since January because results hadn’t been great but we kept working away and got our reward.
"We won enough points over the course of the season, we spent 24 weeks in the top six, and the lads have shown great courage to win the last two games.
"I know it’s a big thing for us because it’s unexpected given the size of the clubs in this division that can outmuscle us a lot of the time.
“I’ve won a couple of promotions in the past but this is as good as them, without a doubt. I’m buzzing that I’ve shared it with good people here.”
Although Hearts dominated possession overall, Motherwell were a different prospect after the break when they were helped by the half-time introduction of Kevin van Veen up front.
Efford should really put Motherwell in front again when Shields clipped a cross to him, but despite getting goal-side of the defender he nodded wide.
Alexander is also hopeful of keeping matchwinner Lamie, who has signed a pre-contract with Dundee but is able to renege on it after the Dens Park club’s relegation to the Championship.
The ’Well boss added: “Are we hopeful on keeping Ricki? Yeah, hopeful is all we can be given the budget.
“We’ve spoken since it came to light about the clause if Dundee go down.
"We want to get these last games out of the way and sit down with him. If we can do it, great.
"Hopefully he's seen that we trust him.”
The match also saw ’Well coach and club legend Keith Lasley – who played over 400 times for the Fir Parkers during two spells – wave goodbye to the home fans ahead of taking over as St Mirren’s chief operating officer this summer.
Motherwell: Kelly, Donnelly, Ojala, Lamie, O’Donnell, Cornelius, Goss, Carroll, Slattery (Tierney 69), Efford (Mugabi 90), Shields (Nirennold 90).
Hearts: Stewart, Moore, Sibbick (Kingsley 75), Cochrane, Atkinson (Smith 75), Haring, Halliday (Simms 63), Woodburn (Thomas 80), Mackay-Steven, Ginnelly, Boyce (Devlin 75).
Referee: Matthew MacDermid