President Trump took aim at two of the world’s most powerful sports leagues and some of their most popular athletes, directly inserting himself into an already fiery debate about race, social justice and the role athletes play in highlighting those issues.

In urging N.F.L. owners to fire players who do not stand for the national anthem, and telling the N.B.A. champion Golden State Warriors they are not welcome at the White House, the president has driven a divide between the players, many of whom are black and opposed to the president’s views on race, and the team owners, who are almost all white and in the N.F.L. largely conservative.

Mr. Trump’s comments, made at a campaign rally on Friday and on social media on Saturday, drew a swift and unusually strong rebuke from the N.F.L., which has done more than most leagues in wrapping itself in the American flag, as well as the players’ union. Scores of football and basketball players, including LeBron James, perhaps the best known athlete in the country, took to social media to lambast the president.

“Going to White House was a great honor until you showed up!” Mr. James wrote on Twitter.

Mr. Trump’s outbursts against athletes and their leagues came as he was smarting from yet another setback in his effort to repeal the Affordable Care Act, and as he worked to stoke enthusiasm among his core supporters in the deeply conservative state of Alabama, where he attended a campaign rally for Senator Luther Strange, who many of them regard as an establishment Republican unworthy of their backing.

The president often uses freewheeling campaign speeches and Twitter to berate and insult critics in unvarnished language. In the past week, he branded North Korea’s leader, Kim Jong-un, as rocket man and criticized Senator John McCain of Arizona for opposing Republican attempts to dismantle the health care law.

But Mr. Trump’s broadsides this time focused on some of the most prominent African-American athletes in the country, who have international followings and have called out the president for his lack of tolerance and divisive views on race.

On Friday night, Mr. Trump said: “Wouldn’t you love to see one of these N.F.L. owners, when somebody disrespects our flag, to say, ‘Get that son of a bitch off the field right now, out, he’s fired,’ ” the president said at the rally, for Senator Luther Strange, who was appointed to the Senate this year and is facing Roy Moore in a Republican primary runoff.

He said the protests would stop if fans left games when players did not stand for the anthem. “The only thing you could do better is if you see it, even if it’s one player, leave the stadium.”

The comments, along with others about the safety of the game, triggered criticism from the league, the union and players. Some people urged more players to kneel or sit during the anthem at football stadiums on Sunday as a way to reinforce their First Amendment rights. Others urged more white players to stand with black players who have knelt or sat during the anthem.

In an unusually strong rebuke of the president on Saturday, Roger Goodell, the commissioner of the league, in which a majority of the owners are Republican, said the president failed to understand how the league and its players work together to “create a sense of unity in our country and our culture.”

“Divisive comments like these demonstrate an unfortunate lack of respect for the N.F.L., our great game and all of our players, and a failure to understand the overwhelming force for good our clubs and players represent in our communities,” he said in a statement.

DeMaurice Smith, the executive director of the N.F.L. Players Association, also took umbrage at the president’s remarks, and added: “The line that marks the balance between the rights of every citizen in our great country gets crossed when someone is told to just ‘shut up and play.’ ”

The president’s comments and the response to them will further inflame a fierce and often uncomfortable debate inside the N.F.L. and among fans about whether the protests disrespect the military and country or are simply an effective way to publicize issues players want to highlight.

Since last season, when the former San Francisco 49ers quarterback Colin Kaepernick began kneeling during the national anthem, the protest has become a litmus test for players, many of whom say they support the protesters but continue to stand for the national anthem. Many coaches and owners have been more explicit, with some all but demanding that players stand for the anthem.

More than half a dozen owners contributed to Mr. Trump’s inauguration, and many of them donate heavily to conservative causes. Some owners, including Robert K. Kraft of the New England Patriots, consider Mr. Trump a personal friend.

Opinions have sharpened in recent months as Mr. Kaepernick, who led the 49ers to the Super Bowl several seasons ago, remains unsigned, leading to charges that the owners have blacklisted him for his political views.

Mr. Goodell, who leads a league in which about three-quarters of the players are black, has tried to find a middle ground. He has said he supports the national anthem, but also believes players have a right to voice their opinion.

The president’s comments on Friday will complicate Mr. Goodell’s efforts to try to appease all parties. While he has reached out to some players, a spokesman on Friday said that it would take time to plan a “social unity month” that some players want so the league can highlight various social issues. The league plans to celebrate military appreciation month in November.

Mr. Trump has a history of antagonizing the N.F.L., dating to the 1980s, when he and the fledgling United States Football League successfully sued it for antitrust violations. Though Mr. Trump won in court, his efforts bankrupted the U.S.F.L. His name surfaced in 2014 as a potential buyer for the Buffalo Bills.

On Friday, Mr. Trump said that the league was losing television viewers in part because it was too focused on safety, including penalizing players for making hard tackles. “They’re ruining the game,” he said.

The president’s comments seemed to embolden players. Detroit Lions tight end Eric Ebron questioned why players were told not to talk about politics, yet the president could speak about sports. “Does anyone tell trump to stick to politics, like they tell us to stick to sports?” he wrote. He added “smh” for “shaking my head.”

Michael Thomas, a defensive back with the Miami Dolphins, urged fellow players not to back down. “Continue to use your voices and your platforms for racial equality and to stop injustices in our communities,” he wrote on Twitter. “This is bigger than us!!!”

By Saturday morning, the president appeared to have another league on his mind. He tweeted that the N.B.A. player Stephen Curry, a two-time M.V.P., was not welcome at the White House.

The team that wins the N.B.A. championship is customarily invited to visit. Mr. Curry’s team, the Golden State Warriors, won this year, but he said on Friday that he did not want to go.

Mr. James, the star player for the Cleveland Cavaliers, responded on Twitter by calling the president a “bum.”

Mr. Curry “already said he ain’t going,” Mr. James said. “So therefore ain’t no invite. Going to White House was a great honor until you showed up!”