NEW YORK — One of the most surprising turnaround stories in recent television history began on one of the most surprising nights in political history.

On Nov. 8, Stephen Colbert was hosting a live election night special for CBS’ sister cable network, Showtime. A program that was built around an expected Hillary Clinton victory went off the rails almost as soon as it went on the air at 11 p.m. As election returns came in, audience members, who had been asked to shut off their phones an hour earlier, gasped as it became clear that Donald Trump could very well become president. Colbert looked dumbstruck.

Sensing the gravity of the moment, Chris Licht, the executive producer of “The Late Show With Stephen Colbert,” walked over to Colbert’s desk during a musical performance.

Advertisement

“Stop being funny and go and just be real,” Licht told the host.

Get The Weekender in your inbox:

The Globe's top picks for what to see and do each weekend, in Boston and beyond.

What followed was what Licht described in a recent interview as the turning point for Colbert, who had struggled to gain his footing on CBS after shedding the pompous-pundit character that made him famous on Comedy Central’s “Colbert Report.”

“I think it’s when he became himself,” he said.

Five months later, “The Late Show” has done what a year ago seemed unthinkable: It has become the most viewed show in late night. Colbert’s show has reeled off nine consecutive weeks of ratings victories over Jimmy Fallon’s once-invincible “Tonight Show,” and is poised to make it 10 in a row when the latest numbers come out Tuesday.

NBC executives have taken solace in the fact that Fallon still commands a lead in the age demographic vital to advertisers, and are skeptical that this Colbert surge will last forever. It is more than possible that Colbert and Fallon, over time, could settle into a battle that will go back and forth.

Advertisement

But at this time last year, Colbert was losing by more than 1 million viewers to Fallon and feeling pressure from within CBS, which had named him the successor to David Letterman with much fanfare. The company’s chief executive, Leslie Moonves, had serious concerns about the show, and the network’s 12:35 a.m. host, James Corden, was outshining him.

And now?

“It’s pizza day,” Colbert said in his 12th-floor office last Tuesday.

Throughout the offices of “The Late Show,” staff members could be heard saying, “Pizza! Pizza!” — celebrating a reward that comes Tuesdays when they beat “The Tonight Show” in the ratings.

Just like “Saturday Night Live” and MSNBC’s prime-time lineup, Colbert has benefited from his decidedly anti-Trump point of view. But even though Trump’s victory appears to have single-handedly turned the late-night comedy race upside down, Colbert’s rise is the product of months of meticulous work. The goal: to earn the chance to be — as Licht put it — “resampled” by viewers.

Advertisement

For its first six months, “The Late Show,” which debuted in September 2015, was adrift. Moonves was concerned enough to express his frustrations to Colbert over dinner at the 21 Club in Manhattan, shortly after a live edition of the show fizzled despite a prime spot immediately after the Super Bowl in February 2016.

Chief among Moonves’ concerns was how uncomfortable Colbert looked on a big stage. He thought the host was worrying over too many trivial details, from the stage lighting down to the color of the dressing rooms.

“On the old show, all of us handled all those responsibilities,” Colbert said, acknowledging that the CBS show was a much bigger undertaking. “And I’m a control freak, and everything — everything — went through my skull.”

Within weeks, Colbert conceded that a change had to be made. And Moonves turned to Licht, an executive producer who had been a career newsman.

“I set up a blind date, and I held my breath,” Moonves said.

Licht, 45, had created a hit as the founding executive producer of MSNBC’s “Morning Joe” and then accomplished something no one had done before him: He created a morning show on CBS that actually drew some ratings.

The TV world rolled its eyes when CBS paired Charlie Rose with Gayle King on “CBS This Morning.” But Licht found success in allowing the co-hosts, along with Norah O’Donnell, to be themselves and to talk freely about the most pressing topics of the day. Viewers responded.

When Moonves approached him about Colbert, Licht said he did not watch the show; he quickly burned through several episodes.

“My cleareyed scouting report was: ‘This is all over the place. This doesn’t seem cohesive, which suggests to me that behind the scenes, it’s chaotic,’” Licht said.

Then he and Colbert sat down for a three-hour drink. They hit it off instantly.

“The deal was, he said, ‘Listen, let me make these decisions and don’t try to take them back from me,’” Colbert remembered. “And I said, ‘OK, well, don’t debate with me what’s funny.’”

So Colbert focused on the comedy and his performance, and Licht dealt with management issues that the host had been expending energy on: staffing, budgets, sales meetings, the works.

Licht also made changes to the show, including shortening the opening credits and giving “The Late Show” a signature segment by preceding those credits with a comedy sketch. Within two months, he suggested regularly doing live shows after major events. If the show was going to become laser-focused on the news, he said, this only made sense. It also brought a necessary rigor to the staff.

Colbert had done, by his estimation, about a dozen live shows over 10 years at “The Colbert Report.” During the past nine months, he has done 15. He pointed to the live shows he did during the political conventions as truly eye-opening.

“Two weeks of that changed all of our approach to the show, and it also changed the trust I had to place in my staff,” Colbert said. “You cannot do two weeks of live shows and be a control freak.”

Colbert became much more forgiving of “a flub here or a flub there,” Licht said.

That is a valuable lesson, said Licht, who believes that certain imperfections foster intimacy with the audience. “At ‘CBS This Morning,’ I said, ‘Guys, there are going to be mornings where Charlie’s tired and he doesn’t smile, and we’re going to have to live with that,’” Licht said. “’Gayle is going to ask questions at the wrong time, and we’re going to have live with that.’”

He continued: “We’re not going to manufacture perfection. Then you build authenticity, and they become more comfortable with each other. Your job as a producer, in my mind, is to allow that to happen and get out of the way.”

Then, in September, on NBC, Fallon tousled Trump’s hair when he was a guest on his show, causing an uproar. Some critics of Fallon say that moment was the breaking point that led to his declining ratings this year.

“The theory that that hair tousle made a difference is based on the supposition that Jimmy’s fans went to him for political acumen,” Colbert said. “I don’t think so. They go there for fun. They go there for his nature, his spirit.”

Colbert and his writing staff, meanwhile, developed a crystal-clear point of view on how they felt about Trump.

Colbert’s election night special on Showtime attracted only 238,000 viewers, fewer than a tenth of his usual viewership.

But in the final minutes of the show, Colbert scrapped a prepared closing monologue about the importance of coming together after a polarizing election, and went off script. He was personal, and he discussed, bluntly, the searing divides in the country.

That moment, Colbert said, was possible only because of the live shows he had done in the previous months.

“That’s when it changed for us,” he said. “And that’s when it started to feel like when you walk off the stage and say, ‘God, what a great freaking job, that I get to do this!’” (He used slightly more colorful language.)

Two weeks into Trump’s presidency, Colbert beat Fallon for the first time. Beyond the political moment, however, Colbert said he felt more comfortable on the Ed Sullivan Theater stage than ever before. “I always had to keep a certain amount of distance as the character,” he said of his time at Comedy Central. “I always had to be a little of a facsimile of me that they were getting — obviously because I was playing somebody named me who wasn’t me, but even on top of that there was a little bit more of a distance from the audience.”

Colbert runs out onto the stage every night these days, and high-fives audience members in the front row. A cameraman circles around him, and Colbert looks directly into the lenses and says, “Hey.” Licht said Colbert had started doing that on his own just about three months ago, a brief, intimate moment between the host and the viewer, watching at home, right before bed.

“I’m so much more comfortable on my feet now,” Colbert said. “I’m a quicker and better writer. I am more comfortable being myself in front of an audience. I like this new relationship with the audience.”