MONTGOMERY, Ala. — Roy S. Moore, a firebrand former chief justice of the Alabama Supreme Court, overcame efforts by top Republicans to rescue his rival, Senator Luther Strange, defeating him on Tuesday in a special primary runoff, according to The Associated Press. The outcome in the closely watched Senate race dealt a humbling blow to President Trump and other party leaders days after the president pleaded with voters in the state to back Mr. Strange. Propelled by the stalwart support of his fellow evangelical Christians, Mr. Moore survived a multimillion-dollar advertising onslaught financed by allies of Senator Mitch McConnell, the majority leader. His victory demonstrated in stark terms the limits of Mr. Trump’s clout. In a race that began as something of a political afterthought and ended up showcasing the right’s enduring divisions, the victory by Mr. Moore, one of the most tenacious figures in Alabama politics, will likely embolden other anti-establishment conservatives to challenge incumbent Republicans in next year’s midterm elections. And more immediately, the party will be forced to wrestle with how to prop up an often-inflammatory candidate given to provocative remarks on same-sex marriage and race — all to protect a seat in a deep-red state. Mr. Moore’s incendiary rhetoric will also oblige others in the party to answer for his comments, perhaps for years to come, at a time when many Republicans would just as soon move on from the debate over gay rights. On Dec. 12, Mr. Moore will face Doug Jones, a former federal prosecutor and the Democratic nominee, in a race that will test the party loyalties of center-right voters who may be uneasy about their nominee. It may also reveal just how reliably Republican the state has become in the quarter-century since a Democrat last won a Senate election here. Mr. Jones said in a statement that Alabama needed a serious senator who would rise above partisanship and work with everyone in Congress. He criticized the debate among Republicans leading up to Tuesday’s election as lacking substance. “I will never embarrass the people of Alabama,” Mr. Jones said. “I am running so the people of Alabama can be proud of their next senator.” But Mr. Moore, 70, has proved himself to be a political survivor. He has been effectively removed from the State Supreme Court twice — the first time in 2003, over his refusal to remove a statue of the Ten Commandments in the courthouse; the second time last year, when he urged the state’s probate judges to defy federal orders regarding same-sex marriage. And in recent days, both the president and Vice President Mike Pence had campaigned for Mr. Strange. Mr. Trump, an enormously popular figure in Alabama, visited the state on Friday, casting aside the tradition of presidents treading carefully in contested primaries, as well as the warnings from his own advisers that he was putting his persuasive powers on the line for a candidate trailing in the polls. Instead of delivering a tightly crafted testimonial, the president rambled for nearly an hour and a half about a range of topics, while openly questioning whether he was making a mistake coming into the state for Mr. Strange, who oriented his entire campaign around Mr. Trump’s endorsement and stood looking on with a red “Make America Great Again” hat atop his head. Mr. Strange conceded defeat on Tuesday night before a subdued audience at a hotel outside of Birmingham, acknowledging in a moment of striking candor that he did not fully grasp the forces at play in his loss. “We’re dealing with a political environment that I’ve never had any experience with,” Mr. Strange said. “The political seas, the political winds in this country right now are very hard to navigate. They’re very hard to understand.” He thanked Mr. Trump effusively, praising the president as a “loyal friend” and attempting to absolve him of any blame for the result. “If this causes him any trouble,” Mr. Strange said, “it’s not his fault.” For his part, Mr. Trump congratulated Mr. Moore in a tweet. “Luther Strange started way back & ran a good race. Roy, WIN in Dec!” he wrote. Mr. Strange’s defeat was the first time an incumbent senator with active White House support has lost since 2010, when Arlen Specter, the longtime senator of Pennsylvania, was beaten in a Democratic primary after switching parties. But his loss was not just a blow to Mr. Trump. Mr. Moore relentlessly linked the senator to Mr. McConnell, who has made a priority of protecting his caucus from intraparty challenges, but he is an increasingly polarizing figure among grass-roots Republicans. Despite the money and staff he directed to the race, Mr. McConnell became as much a liability as he was an asset, leaving Republicans nervously wondering what that may portend in other primaries next year. Mr. McConnell and his allies were jolted with another reminder of their limited control on Tuesday, when Senator Bob Corker of Tennessee, a popular incumbent, announced that he would not run for re-election. As the first senator to opt out of seeking another term in 2018, Mr. Corker opened the way for another rowdy Southern primary in which the national party’s influence may be sorely tested. Mr. Strange’s demise was in some respects as much a local phenomenon as a national one, stemming from his appointment this year by then-Gov. Robert Bentley to fill the seat vacated by Attorney General Jeff Sessions. Mr. Strange, the state’s attorney general at the time, was overseeing an investigation into Mr. Bentley’s personal relationship with a close aide, suggesting to many in a scandal-weary state that there may have been a corrupt bargain. The newly appointed senator denied any wrongdoing, but never fully confronted the issue in a way that would eliminate the lingering cloud over the appointment. And by Monday, an adviser to Mr. McConnell, anticipating defeat, started to privately make the case that it was Mr. Bentley’s scandal and the circumstances around the appointment that was most to blame for Mr. Strange’s lackluster support. When the Alabama race started, it was with less fanfare, merely a side effect of Mr. Trump’s selection of Mr. Sessions as attorney general. Republicans typically win federal races in Alabama without difficulty, so there was little immediate concern about the fate of Mr. Sessions’s seat, and less still after the appointment of such a conventional politician as Mr. Strange. Mr. Strange’s status as a proxy for the Republican establishment and a test of the president’s sway came about almost by accident — a consequence of factors having little to do with Mr. Strange himself. Seeking to ward off insurgents like Mr. Moore and Representative Mo Brooks, who finished third in last month’s primary, Mr. McConnell forcefully backed Mr. Strange’s bid to have his appointment affirmed by voters. The Senate Republican leader treated Mr. Strange as the political equal of his elected colleagues and ordered strategists in Washington not to work against him. Mr. McConnell and a host of other senators lobbied an initially reluctant Mr. Trump to get involved on Mr. Strange’s behalf over the objections of some advisers. The confusing crosscurrents of the party were on vivid display when the president campaigned for Mr. Strange on Friday. As staff members from the party’s campaign arm allied with Mr. McConnell looked on, Mr. Strange told the conservative audience that they should elect him so he could “stand up to” Mr. McConnell. And then the president took the stage and assured attendees he would back Mr. Moore were Mr. Strange to lose, comments that were soon made into an online ad by an anti-establishment conservative group.