First and foremost they are peace officers licensed by the State, secondly they get hired by the municipal corporations. Maybe the solution is that we should be hiring them instead of letting the municipalities gobble up the majority of them. Before most any cop is hire-able by a municipality, they get training and certification (ala POST / Peace Officer Standards and Training) to become a peace officer for a given State. Once the municipalities employ them, they become police--hired to enforce municipal policy. That is why one does not typically see sheriffs involved in issuing traffic tickets or issuing tickets for how tall someone's grass is or is not.
The sheriff, the sheriffs deputies, court clerks and the judges are the peace officers or conservators of the peace who are typically outside of the municipal corporation paradigm. Key is in not allowing them to incorporate everything or do away with sheriffs--keeping sheriffs and non-police peace officers is highly important.
In a county "gobbled up" by municipalities (such as Five Burroughs of New York)--the 'populace' is presumed dead. It seems that the sheriffs, his clerk and the court judges would remain outside of the municipal paradigm. Sheriffs tend to wear brown (which is like red) and gold which is said to signify they being for or of the 'living' or the de jure authority. Police wear blue, black or green to signify they are on the civilly-dead or corporate side. Sheriffs are not police. In the case were sheriffs wear blue, I have found they are typically part of a unified city-county government system.
The sheriff traditionally serves the role of the king's local agent or of principal conservator of the peace for a given county (a county officer with a county being a subdivision of a state).