TOKYO—The Tokyo Olympics will come to an end with the closing ceremony for the Games on Sunday after 17 days in which the world’s biggest sporting event coped with a new, aggressive wave of Covid-19 and a Japanese public that remained uninterested to the end in the role of host.

Spectators were banned at most events and visitors’ movements subjected to unprecedented restrictions in an effort to contain the virus at the expense of its usual celebratory atmosphere. World records fell and competitive dramas unfolded in arenas populated sparsely with team officials, Olympic volunteers and media.

It largely worked. Even though dozens of athletes, officials and workers tested positive for Covid-19 and the number of infections across Tokyo reached new highs during the Games, there has been little evidence yet that the Games led to a substantial spread of infections either inside or outside the event. That was the biggest fear cited by those who had wanted the Olympics canceled.

Yet the Olympics were a far cry from what Japan’s former leader, Shinzo Abe, who brought the Games to Tokyo, said they would be—the Olympics in their full-fledged form, demonstrating humankind’s triumph over the coronavirus. On the contrary, the virus hung over every moment of the Games and asserted its presence in every picture of empty stands.

The pandemic in Japan got worse every day during the Olympics, including in Tokyo where a state of emergency has been extended until the end of August.

Photo: jorge silva/Reuters

The public’s overriding concern with the pandemic—and continuing ambivalence toward the Olympics—may mean Prime Minister Yoshihide Suga, whose job is on the line in national elections later this year, won’t see any boost from Japan’s record medal haul. The host nation lost billions of dollars without the boost of foreign tourism and ticket sales.

The setting took a toll on the world’s greatest athletes, who competed and won medals—and sometimes were overwhelmed by the pressure of training an extra year to make it to the Games and then perform in lifeless arenas.

Naomi Osaka, the Japanese tennis star who the hosts hoped would be the face of the Games, instead made an early exit, citing the pressure on her. Novak Djovokic savagely destroyed his tennis racket as he found himself losing consecutive matches in the brutal heat, leaving without a medal.

Japan’s Naomi Osaka, who the hosts hoped would be the face of the Games, lost in the third round.

Photo: Seth Wenig/Associated Press

No surprise was bigger than Simone Biles’ decision to pull out of the gymnastics team competition after it was under way, citing a disorienting condition that had rendered her unable to compete safely and effectively. Biles days later made a dramatic return at the gymnastics competition to win a bronze medal on the balance beam.

Yet the Games still managed to generate some of the drama and energy the event is accustomed to. Records were set—two men and two women broke the world-best times in their respective 400-meter hurdles races—and dynasties such as the U.S. women’s basketball team were extended. Two high jumpers agreed to share a gold medal rather than enter a jump-off to settle matters.

Those who made it to Tokyo faced the pressure of navigating the health protocols that were imposed to protect Japan’s public. Not all of them succeeded.

A total of 29 athletes tested positive for Covid-19 and were isolated, forcing them out of competition. Among them were some medal contenders, including American pole vaulter and reigning world champion Sam Kendricks. Those who tested positive were sent off to isolation under conditions that drew complaints for their severity.

Sydney McLaughlin won the gold in the women’s 400-meter hurdles.

Photo: Kyodonews/Zuma Press

In Japan, the pandemic got worse every day during the Olympics, driven by the Delta variant. On Saturday, the government said the cumulative total of Covid-19 infections since the pandemic began had exceeded one million, after doubling in just four months. The spread of the virus is most severe in Tokyo, where a state of emergency has been extended to the end of August.

The government’s top medical adviser on Covid-19 said that while there was no direct connection between Japan’s surge in infections and the Olympics, the festive mood of the event may have led the public to let down its guard.

Few clusters of infection emerged in the Olympic Village as daily testing and rules on social distancing and hygiene were enforced among around 11,000 athletes. Greece’s artistic swimming team withdrew after six members tested positive but no events had major disruptions.

“We have shown it is possible to keep a pandemic at bay,” said Brian McCloskey, the head of a panel of experts who advised on Covid-19 countermeasures at the Olympics.

Just over 400 infections were confirmed among around 190,000 people working at the Games.

“Overall we can be happy, satisfied and confident,” said International Olympic Committee President Thomas Bach on Sunday. “Let us enjoy today the great success of the Olympic Games Tokyo 2020.”

In the host nation, ambivalence prevailed. Tens of thousands of ticket holders were locked out and, despite Japan winning a record 58 medals, the Games fell short of electrifying the host nation.

Games organizers said nine out of 10 Japanese watched some part of the event on television or online. But on buses and trains, few appeared to be making use of free streaming services to watch events.

Judo star Shohei Ono won one of Japan’s nine gold medals in the sport.

Photo: annegret hilse/Reuters

As judo star Shohei Ono won one of Japan’s nine gold medals in the sport, bars observing government requests not to serve alcohol were mostly empty. At those that offered alcohol, TVs showing the Games usually weren’t the focus of attention.

Opposition among Japanese to the Games eased going into the event but a small majority were still against the Games even as Japan raced up the medal table, according to polls taken on July 27-28 and Aug. 4-5 by Osaka University.

Still, more than half of households in Japan watched the opening ceremony, and the rush of medals prompted many people to search for souvenirs of the Olympics. At official stores and discount retailer Don Quijote, merchandise with the Tokyo 2020 logo sold well.

“We’re nearly running out of a lot of things,” said Futoshi Otagoshi, manager of the Don Quijote store in the Shibuya shopping district, pointing to a rack with only one or two key holders left.

These modest signs of engagement from the public aren’t likely to alter the post-Olympics political equation in Japan.

“I expect that the surge in Covid cases and the stresses on the medical system will offset whatever gain Suga might have gotten from the Tokyo Games,” said Tobias Harris, an expert in Japanese politics at the Center for American Progress, a Washington, D.C., think tank.

Eight new permanent venues built for the Olympics cost Japan more than $3 billion but had only empty seats during the Games. The most expensive—the National Stadium—has no publicly listed events after the Paralympics, which begin on Aug. 24.

Japan is also left to cover the losses from roughly $800 million in missed ticket sales.

Simone Biles’s withdrawal from competitions at the Tokyo Olympics has put renewed focus on mental health in sports. WSJ looks at how the stigma and treatment for athletes’ state of mind has shifted. Photo: Mike Blake/Reuters The Wall Street Journal Interactive Edition

In the world of sports, the Games marked a milestone in the way athletes spoke out publicly about their mental health. Biles, the former gold medalist in gymnastics, said she feared for her safety if she attempted the gravity-defying maneuvers for which she is known, an unprecedented acknowledgment of vulnerability.

U.S. swimmer Caeleb Dressel broke down in tears after winning the first of his five gold medals at the Games, saying he’d been dogged by self-doubt. Japanese officials credited a stress-reduction effort for the nation’s strong showing at the Games.

For many athletes and coaches, the decision to hold the Games meant they got their moment on the greatest stage. Despite the inconveniences of constant testing and restrictions on movement, they said it was worthwhile.

“We can talk about some things that are negative, but the bottom line is, it says ‘USA’ across our jersey, and these guys are playing for a gold medal this weekend. It doesn’t get any better than that,” Mike Scioscia, manager of Team USA baseball team, said before the final game against Japan.

The host nation won, 2-0. 

Write to Alastair Gale at alastair.gale@wsj.com and Rachel Bachman at rachel.bachman@wsj.com