When the St. Louis Cardinals woke up on July 30, the morning of baseball’s annual trade deadline, they appeared to be out of contention. Their record had been hovering around the .500 mark for six weeks, and they were eight games out of a National League wild-card spot. The statistics website FanGraphs gave them a 2% chance of advancing to the postseason.

The San Diego Padres, meanwhile, were cruising. They were comfortably on their way to surpassing 90 victories and seemed destined for October, living up to the immense hype...

When the St. Louis Cardinals woke up on July 30, the morning of baseball’s annual trade deadline, they appeared to be out of contention. Their record had been hovering around the .500 mark for six weeks, and they were eight games out of a National League wild-card spot. The statistics website FanGraphs gave them a 2% chance of advancing to the postseason.

The San Diego Padres, meanwhile, were cruising. They were comfortably on their way to surpassing 90 victories and seemed destined for October, living up to the immense hype that followed them all winter and justifying their World Series aspirations. Their playoff odds were just shy of 90%.

What’s happened since is nothing short of shocking. The Cardinals have emerged as an unexpected juggernaut, putting together a historic run that has vaulted them to the precipice of the postseason.

After beating the Chicago Cubs on Sunday afternoon, the Cardinals have now won 16 consecutive games. It’s the longest winning streak in franchise history and the longest for a National League team since 1951. With a week remaining in the season, the Cardinals are on the verge of clinching the second and final NL wild-card slot, an achievement that seemed all but impossible just a few weeks ago

As for the Padres, they have collapsed so monumentally that it has cast doubt on their much-heralded future and will almost certainly lead to significant organizational changes. With a loss to the Atlanta Braves on Saturday, the Padres were officially eliminated from playoff contention, a dismal end to a season that began with such promise.

It’s a stunning reversal of fortune for both teams that has reshaped the trajectory of the season in its final moments.

The Padres’ Fernando Tatis Jr. reacts after a strike out.

Photo: Gregory Bull/Associated Press

“This is crunch time, and we’re the Cardinals,” outfielder Tyler O’Neill said earlier this month. “September baseball. Stuff’s important. Here to win. That’s what we do.”

He’s not wrong. The Cardinals have won more games than any other National League team this century and are closing in on their 15th playoff appearance over that span. They have participated in four World Series and won championships in 2006 and 2011. This is, in fact, what the Cardinals do.

It just wasn’t supposed to happen this year. Statistically, the Cardinals have had, at best, an average offense and pitching staff in 2021. All told, they’ve barely scored more runs than they’ve allowed. even with the winning streak that brought them back to life. 

Yet here they are, on the verge of completing a turnaround the Cardinals swear they saw coming—even if nobody else did.

“There were frustrations as the days ticked off the schedule,” Cardinals president of baseball operations John Mozeliak said in an interview last week. “But there was always this belief that we were going to be OK.”

The Cardinals felt that way in part because of their hot start. They spent nearly all of May in first place in their division, before a disastrous, injury-riddled June dropped them far outside the postseason conversation. 

Nonetheless, Mozeliak thought there were still reasons that 2021 could be salvaged. Adam Wainwright, now 40-years-old, is having his best season since his last All-Star appearance in 2014, posting a 3.05 ERA in his 31 outings. O’Neill has been a breakout star, surpassing 30 home runs. Third baseman Nolan Arenado, in his first season with St. Louis after arriving in a trade with the Colorado Rockies, has topped 30 homers and 100 RBIs for the sixth time in his career. First baseman Paul Goldschmidt is also on his way to 30 home runs and 100 RBIs.

So at the trade deadline, Mozeliak decided to seek help, acquiring veteran starters Jon Lester and J.A. Happ. The moves generated little attention at the time but have been crucial. Happ has surrendered three earned runs or fewer in eight of his 10 starts with the Cardinals. Lester has a 3.07 ERA this month.

“We still thought there were some things we could do to help the club and still right this ship,” Mozeliak said. “Part of the 162-game season is patience because lots of things can happen over time.”

Giovanny Gallegos of the St. Louis Cardinals celebrates after beating the San Diego Padres on Sept. 19.

Photo: Dilip Vishwanat/Getty Images

None of this would have mattered if not for the Padres’ meltdown, an unequivocal debacle that has now combusted: Last week, during a game in St. Louis, a video circulated widely on social media of Manny Machado, the Padres’ $300 million third baseman, screaming in the dugout at Fernando Tatis Jr., their $340 million shortstop.

The two players say they’ve reconciled and that the issue is behind them, but the episode provided an unfiltered glimpse into a team that’s in free-fall. Through Saturday, the Padres had gone 6-15 in September, tied for the worst in baseball.

“You can’t pinpoint any one thing,” Machado told reporters Saturday. “It’s a long season. A lot of things happened. We didn’t play good baseball, and we got injured.

The Padres came into the season with visions of dethroning the Dodgers after eight consecutive division crowns. They landed pitchers Yu Darvish and Blake Snell in trades and paired them with Tatis and Machado to build what looked like a powerhouse. 

Tatis has been as superb as anticipated, battling with Bryce Harper of the Philadelphia Phillies for MVP honors. Machado has been a worthy sidekick. Just about everything else, however, has gone wrong. 

Snell’s ERA is 4.20. Darvish’s is 4.21. The Padres have had so many injuries that they’ve been forced to scrounge the scrap heap for castoffs like Jake Arrieta and Vince Velasquez just so they have somebody to put on the mound. 

They fired their pitching coach in late August and have since begun to overhaul their scouting and player development departments. Their manager, Jayce Tingler, is also likely in danger after a roster with so much promise failed to reach its potential. Instead of vying for the playoffs, the Padres are just trying to finish the season with a winning record.

“I thought this was the year we’d get going and really start to roll,” Padres outfielder Wil Myers told reporters. “That wasn’t the case.”

Manny Machado and Fernando Tatis Jr. exchanged words in the dugout during a game against the Cardinals on Sept. 18.

Photo: Jeff Curry/Getty Images

While the Padres faded, the Cardinals rose, taking advantage of a sudden opening in the playoff picture. Their road will be treacherous, beginning with the do-or-die wild-card game against either the Dodgers or San Francisco Giants, the teams with the two best records in the majors.

But the way the Cardinals are playing right now, anything seems possible. They might not be the best team—but with the playoffs rapidly approaching, nobody is playing better.

“Confidence can be a scary thing,” Happ said. “When you’re playing right at the right time of the year, sometimes that can go a long way.”

Write to Jared Diamond at jared.diamond@wsj.com