Ed Lee, the son of Chinese immigrants who rose to become the first Asian American mayor of San Francisco in 2011, died early Tuesday after a heart attack.

Former mayor Willie Brown told the San Francisco Examiner that Mr. Lee was shopping at his neighborhood Safeway when he suffered the attack. Mr. Lee died just after 1 a.m. at Zuckerberg San Francisco General Hospital, with friends, family and colleagues by his side, the city said.

The 65-year-old Democrat — an affordable-housing advocate who led the city during a time of ballooning rents and explosive real estate prices — was remembered by political leaders as a defender of civil rights. This year, Mr. Lee clashed with President Trump over the city’s protections of undocumented immigrants.

California Gov. Jerry Brown (D) called Mr. Lee “a true champion for working people.”

Mr. Lee, an activist lawyer before he began working for city agencies, was “one of America’s most passionate champions for climate action,” former New York Mayor Michael Bloomberg said in a statement.

San Francisco Board of Supervisors President London Breed, who is now acting mayor, said Mr. Lee lived “a life of service . . . cut short far too soon.” Speaking at a City Hall news conference Monday morning, she said, “Ed Lee fought against discrimination, working on the front lines to keep tenants from being evicted. He was, from the dawn of his career, an advocate for the powerless, a voice for the overlooked.”

The San Francisco Chronicle editorial board remembered Mr. Lee as “a mayor who calmed S.F. City Hall.” “For a man who entered City Hall’s Room 200 with no particular appetite for the rough and tumble of elective office, Ed Lee proved remarkably adept at navigating and bridging the divisions,” the Chronicle said. “He set a temperate tone for a city that desperately needed it.”

Under Mr. Lee, who became mayor when his predecessor, Gavin Newsom, left to become California’s lieutenant governor in January 2011, the city pushed through the 2011 “Twitter tax,” which sliced payroll taxes for businesses that relocated to San Francisco’s Mid-Market district. The business-friendly policies helped lure tech companies, bringing a wave of new, wealthy residents. Mr. Lee spent the rest of his time as mayor trying a variety of measures to make housing affordable for all San Franciscans, as housing costs soared to heights only attainable by the wealthy.

He also accomplished what he referred to as his “legacy project,” convincing the NBA’s Golden State Warriors to relocate from Oakland to a billion-dollar arena in San Francisco in 2019.

“His love and passion for sports, including the Warriors, defined him as much as his witty humor and engaging personality,” the team said in a statement. “We will be eternally grateful for his commitment to the building of Chase Center.”

Edwin Mah Lee was born May 5, 1952, to immigrant parents who came to the United States from the Chinese province of Guangdong and settled in Seattle. He told the Northwest Asian Weekly that he was the fifth of six children in a home where both parents worked — his father in local restaurants, his mother doing odd jobs around Seattle. When Mr. Lee was 15, his father died of a heart attack, and Mr. Lee worked in restaurants to help support his family.

Mr. Lee was the first member of his family to attend college and graduated from Bowdoin College in Maine in 1974. Four years later, he received a law degree from the University of California at Berkeley, where he also became interested in politics.

Mr. Lee worked for the San Francisco Asian Law Caucus, advocating for immigrant rights and affordable housing. He later joined city government, leading the Human Rights Commission and the Department of Public Works, among other agencies.

He was serving as city administrator when he was appointed by the Board of Supervisors to replace Newsom. In a tweet on Tuesday, Newsom said Mr. Lee’s “intellect, integrity, boundless optimism & contagious love elevated our City.”

More than a third of San Francisco’s roughly 870,000 residents are Asian, according to census data, and Mr. Lee said his election in November 2011 was a stride toward equality. “I am able to make a link to the Asian communities,” he told Northwest Asian Weekly. “Being mayor helps them to know that they no longer are second-class citizens.”

Before the 2011 election, the San Francisco Chronicle reported that “there are roughly 10,000 Lees in San Francisco, an expected boost for the mayor at the ballot box.” Mr. Lee won in a crowded field of 16 candidates, then coasted to reelection in 2015.

“Mayor Lee took deep pride in serving as the first Asian American mayor of San Francisco,” said House Minority Leader Nancy Pelosi (D), who represents San Francisco in Congress. “His greatest source of joy was his beloved family, and our city owes a debt of gratitude to his wife, Anita, and his daughters, Brianna and Tania, for sharing this exceptional, lovely person with us.”

Mr. Lee became known as one of the most progressive mayors in the United States, who clashed with Trump over San Francisco’s designation as a “sanctuary city.” Under the policy — one of the most expansive in the country — local police won’t cooperate with federal immigration officials in all but the most extreme cases.

In January, an hour after Trump announced a federal crackdown on undocumented immigrants, Mr. Lee held a news conference at City Hall. “I am here today to say we are still a sanctuary city,” he said, according to the Chronicle. “We stand by our sanctuary city because we want everybody to feel safe and utilize the services they deserve, including education and health care . . . It is my obligation to keep our city united, keep it strong . . . Crime doesn’t know documentation. Disease doesn’t know documentation.”

The conservative news outlet Breitbart deemed Mr. Lee “somewhat controversial” for the stance, noting that Mr. Lee stood by the policy “even after the killing of Kate Steinle by an illegal alien who had been deported five times already and had deliberately moved to the city to avoid deportation again.”

TechCrunch said Mr. Lee “positioned himself as an advocate to attract and keep tech companies in the city.” His aim was not simply to get tech companies to come to San Francisco, TechCrunch said, but to “leverage the wealth of that industry to try to address the city’s problems.”

Political leaders remembered a leader who had been fighting for San Franciscans for decades.

Sen. Dianne Feinstein (D-Calif.), who served as the city’s mayor in the late 1970s and 1980s, called Mr. Lee’s death “a very sad day for San Francisco and all of us who knew Ed.” She said Mr. Lee “was an excellent mayor of a great but sometimes challenging city. His equanimity and quiet management style was effective and allowed him to solve problems as they occurred.”

Sen. Kamala D. Harris (D-Calif.) called Mr. Lee “a public servant who tackled every challenge with modesty, civility and hard work. As the son of immigrants who became mayor of one of America’s largest cities, Ed broke down barriers and blazed a trail for future generations to follow. And at this inflection moment in our country when some have promoted hatred and division, Mayor Lee has been an outspoken advocate for diversity and inclusion.”

Harris added that “when he first ran for mayor, Ed campaigned on the message, ‘Ed Lee Gets It Done.’ For 65 remarkable years, he did.”

Mr. Lee’s death comes nearly three decades after another San Francisco Mayor, George Moscone, died in office.

Moscone was fatally shot two years into his term, along with San Francisco supervisor Harvey Milk, by Dan White, a former member of the Board of Supervisors.