By Dave Sebastian and Sharon Terlep

Walmart Inc. will require customers to wear face coverings inside all of its U.S. Walmart and Sam's Club stores starting July 20, becoming the largest company to impose restrictions amid a surge in coronavirus cases in several U.S. states.

A patchwork of local rules have left businesses to enforce their own mask policies to protect their staff and customers against the pathogen, which has killed more than 135,000 Americans. Walmart joins Costco Wholesale Corp., Best Buy Co. and Starbucks Corp. in having a nationwide mask requirement.

About 65% of Walmart's more than 5,000 U.S. locations are in areas where a government mandate on face coverings is in place, said Dacona Smith, operating chief of Walmart U.S., and Lance De La Rosa, Sam's Club chief operating officer.

Walmart said in states and cities with mask mandates "virtually everyone either brings a mask or readily complies with the requirement, and we anticipate that to happen in other areas as well."

There is widespread scientific and medical consensus that face masks are critical in slowing the virus's spread. Questions over wearing them have fueled heated political debates in the U.S. and led to sometimes violent confrontations between retail workers enforcing mask rules and customers who refuse to cover their face.

Earl Lagorin, an 81-year-old Colorado resident, said he stopped shopping at one Walmart outside Denver because he didn't find the wearing of masks to be adequately encouraged. "I think it's wonderful if they can enforce" the mask policy, he said.

The Centers for Disease Control and Prevention recommends that everyone wear a face covering in public, especially indoors and when social distancing isn't possible. The White House Coronavirus Task Force has left any mask mandates to states.

Texas, California and other states with major outbreaks mandate masks in public but about half of U.S. states don't, including places like Florida where there has been a recent surge in cases and Walmart's home state of Arkansas. Many local governments have mandates in states where they aren't required.

The differing rules have prompted business leaders to call on the White House and state leaders to adopt a national mask policy. "Issuing voluntary guidance on masks is insufficient to protect public health" and risks continued community spread and new round of shutdowns, the U.S. Chamber of Commerce, National Retail Federation and other business groups wrote in a July 2 letter to the White House and governors.

"Retailers are protecting the health and safety of communities they serve," the NRF said Wednesday. "Our elected leaders need to set politics aside and follow their example." The industry group called on all retailers to adopt nationwide mask policies, saying Walmart's decision was a tipping point in the debate.

The United Food and Commercial Workers International Union, which represents 1.3 million workers in grocery, retail, and other industries, has also called for a national mask mandate. "With governors in the majority of states refusing to make masks mandatory, millions of Americans are needlessly being put in danger every day," said Marc Perrone, the UFCW International president.

Most major companies, from big-box retailers to pharmacy chains, require employees to wear masks and ask shoppers to wear them in places where state or local governments say they are mandatory. Some of those companies prohibit patrons from entering without a mask, while others request customers wear one but will still allow them in if they refuse.

Walmart said it would have staff members near the entrance to remind visitors who aren't wearing a mask and hand out free masks to those who didn't bring one, and will make some exceptions to the requirement. "Our associates will be trained on those exceptions to help reduce friction for the shopper and make the process as easy as possible for everyone," Messrs. Smith and De La Rosa said.

Stores will have a single entrance to ensure the requirement's consistency.

Walmart's executives said they recognize people have different opinions on the topic but the company "recognizes the role we can play to help protect the health and well-being of the communities we serve by following the evolving guidance of health officials like the CDC."

The retailer's U.S. stores have been open throughout the pandemic and the company has required workers to wear masks since mid April.

Costco has required masks for shoppers at its warehouse stores since May and became a target for mask opponents. The company's finance chief said at the time that some shoppers objected but overall customers liked the safety measure. Apple Inc. has also required masks for shoppers in its stores.

Best Buy said it would provide masks to shoppers who didn't have them and those who didn't wish to wear one could shop online or opt for its curbside service. A spokeswoman declined to comment on whether the chain will enforce its mask mandate, which went into effect on Wednesday.

Pharmacy chains CVS Health Corp. and Walgreens Boots Alliance Inc. require masks in places where local regulations mandate them, but have instructed employees not to prohibit customers from entering if they refuse. Target Corp. store workers ask shoppers to wear one, often offering a mask for sale near the entrance, but are asked not to force the issue for safety reasons, said a person familiar with the company policy.

More than 3 million Americans have been infected by coronavirus and the number of new cases have hit highs in recent days, according to data compiled by Johns Hopkins University. California's governor this week reversed plans to reopen businesses amid a spike in the state, ordering indoor dining and malls to close again.

Write to Dave Sebastian at dave.sebastian@wsj.com and Sharon Terlep at sharon.terlep@wsj.com