Older white and conservative voters propelled him to victory every four years for six elections, pumping millions of dollars into his campaign. But his dominance waned as he faced more legal challenges and opposition from a growing number of Latinos, who this year accounted for almost 20 percent of all registered voters in the state. Latinos are poised to become a majority in Arizona by 2030.

Even as Maricopa County, the state’s largest, spent tens of millions of dollars in Sheriff Arpaio’s legal defense — he has also faced numerous lawsuits over abuse and faulty medical care in the several jails he runs — he claimed that he saved taxpayers money.

His focus on immigration enforcement intensified as hundreds of thousands of undocumented immigrants were crossing into Arizona. For a time, the federal government enabled his activities, allowing some of his deputies to act as immigration agents and, with that, to selectively target Latinos on the streets and at work.

Sheriff Arpaio plunged into the illegal immigration debate in 2005, after an Army reservist named Patrick Haab detained seven undocumented immigrants at gunpoint in the desert. The sheriff ordered Mr. Haab arrested, saying, “Being illegal is not a serious crime.”

The Maricopa County attorney at the time, Andrew Thomas, refused to prosecute Mr. Haab, though, holding him up as a sort of folk hero who was simply exercising his rights as a citizen.

“You could almost see a light bulb go off as Arpaio watched the positive reaction from the public,” recalled Paul K. Charlton, who served as United States attorney for Arizona from 2001 to 2007. “From that point on, we lost him.”

In 2005, an Arizona law made it a felony to smuggle people for profit in the state, and Mr. Thomas, who has since been disbarred, interpreted the law as one that criminalizes both smugglers and those being smuggled, effectively giving the sheriff carte blanche to carry out his arrests of unauthorized immigrants.