WASHINGTON—The Supreme Court upheld a federal law requiring TikTok’s Chinese owners to sell or shut down the social-media app by Jan. 19, siding with Congress’s national-security concerns over the platform and its users’ claim that the ban violates the First Amendment.

The ruling means the platform could go dark—at least temporarily—on Sunday, depriving millions of teenagers and other TikTok users of their daily fix of short-form videos that keep them glued to their phones.

President-elect Donald Trump and his allies are trying to find a political path forward to assuage security concerns and rescue the app. Biden administration officials have signaled they don’t intend to enforce the ban before leaving office, but that hasn’t been enough to give TikTok comfort.

If it lost at the Supreme Court, TikTok has been planning to shut down the app in the U.S. to comply with the law and avoid exposing companies that sell or distribute the app to legal liability. It’s also been exploring other maneuvers and courting Trump.

With the deadline rapidly approaching, the court acted with unusual speed, holding a special day of arguments on Jan. 10, weeks after a federal appeals court upheld the TikTok ban . The Supreme Court was the last stop for TikTok, a subsidiary of Beijing-based ByteDance, and a group of Americans who use the app and say no comparable platform currently is available to them.

The bipartisan bill President Biden signed last April classifies the app, which serves up short videos to some 170 million American users each month, as the tool of a foreign adversary that poses a grave threat to national security. Justice Department lawyers argued that the platform poses two distinct dangers: It collects a trove of personal data on tens of millions of Americans that will remain at the disposal of Chinese intelligence for decades to come, and it allows Beijing to undermine U.S. democracy by manipulating the information Americans see.

Although ByteDance is a private company, the Justice Department says that it effectively is an arm of China’s Communist government, which closely supervises business entities and requires their cooperation with security services.

Lawyers for TikTok and its users argued that the government was abridging free-speech rights based on speculation, and that any risks the app might pose could be addressed through less restrictive means. More detailed consumer disclosures about the app’s ownership and legal penalties for transferring user data to China were options that Congress failed to explore, TikTok advocates argued.

Celebrities, fashion trends, videogames and other light fare predominate among TikTok topics, but the app increasingly is a source of news, research shows, particularly for younger Americans. According to the Pew Research Center, 45% of Americans between the ages of 18 and 29 regularly get news on TikTok, while 4% of those 65 and older do.

TikTok’s hopes rested with a line of Supreme Court cases that have guarded First Amendment rights in the internet age. Justices across the ideological spectrum have rejected claims that the endless frontier of cyberspace requires more restrictive approaches to free speech than sufficed when mass media meant handbills, printing presses and broadcast towers.

But the same Supreme Court has deferred to the government when it comes to national security and foreign policy, declaring that judges lack the capacity and constitutional authority to second-guess executive determinations involving defense of the U.S. and its interests from overseas threats.

Write to Jess Bravin at Jess.Bravin@wsj.com