It's all a question of jurisdiction.
Police refers to officers within a particular municipality. For example, the city of Houston has its own police force, which is charged with enforcing laws within the city itself. If you go out to a neighboring city, say Sugar Land, that city has its own police force.
Sheriffs are officers with jurisdiction over a county. The actual authority and jobs of a sheriff varies wildly from state to state, and even county to county. Some sheriffs have their own police forces, while others perform largely ministerial roles. In most states, the county sheriff is required to run a jail and provide services to the courts, such as security and the service and enforcement of court orders. The title "sheriff" is a contraction of "shire reeve," who was the tax collector in feudal England. In some states, the official title of the sheriff is "sheriff and tax collector."
A Ranger (in the Texas Ranger sense) is an officer with statewide authority. In Texas, the rangers handle major/high-profile crimes, serial crimes, officer-involved shootings , etc. Many other states have similar agencies, although their officers are not called rangers, e.g., Oklahoma State Bureau of Investigation, Georgia Bureau of Investigation.
A marshal is a federal officer whose job is to support the federal judiciary. US Marshals track fugitives, protect federal courts, transport federal prisoners, and serve federal warrants, among other things.
Federal law-enforcement enforces federal laws and punishes federal crimes. That sounds like a tautology but it's really not that simple in practice. Some crimes are defined as crimes nationwide: they are Federal crimes. If the law being broken was passed by Congress, that's a federal offense.
Federal law enforcement agencies automatically have jurisdiction over federal offenses. They can be invited in by local forces if the local force wants the resources a federal enforcement arm can bring to bear, but they need no invitation if it's "their" crime. The feds also have automatic jurisdiction if the crime occurred on federally-owned land: national parks, in federal office buildings, etc. If the place or the crime is federal, they have jurisdiction.
The Secret Service protect the president, but they are also the law enforcement agency with jurisdiction over counterfeiting of the US currency.
The ATF (Bureau of Alcohol, Tobacco, Firearms, and Explosives) have jurisdiction over federal crimes where someone has violated federal laws about any of the things in the name of their agency.
Fish and Wildlife have jurisdiction over violations of wildlife regulations, like hunting out of season or otherwise infringing on the rules about endangered species.
The FBI are a general-purpose federal police force, with sub-bureaus for a whole bunch of incredibly specific stuff.
The CIA is not supposed to operate on US soil; they are an enforcement and spying arm that aims outwards from the borders and protects US interests abroad. But sometimes they are naughty and step on things that should be the FBI's bailiwick, which often leads to drama in fiction (and sometimes in real life).
The NSA are almost entirely information-gathering, you won't have them kicking down a door.
Customs and Border Protection does what it says on the tin, though a lot of things have been made "their problem" in the post-9/11 era that seem a weird fit.
Immigration is part of CBP, but is also its own separate sub-bureau dealing with visa overstays and general people-here-who-shouldn't-be.
Mail fraud automatically makes a crime that involves it federal, because any time any aspect of the crime goes through the US Postal Service, you've just given Feds jurisdiction.
Jumping down to the smallest jurisdictions, most non-federal stuff is dealt with by municipal police forces -- for one city/town. Unincorporated areas, that are not inside a town's borders, are covered by county sheriffs (a lot of rural areas are sheriff-first for this reason). The state highway system and all other state-owned land are policed by state troopers, who have a sub-department sometimes called "highway patrol".
As with the feds, states have automatic jurisdiction on state land or for crimes passed by the state legislature, if they choose to exercise it, but usually they let ordinary criminal code stuff be handled by the locals, because they don't have the staff to police everything and everywhere.
Some movies and television shows are very careful about portraying jurisdiction (it is often a plot point on Criminal Minds that they can't parachute in and take over until either the locals request it or the crime goes federal), but a lot just wave their hands around and do whatever the writer thinks would make a good story.
For example, if your protagonists are local detectives, you can make their lives difficult and add drama by having the Feds "take away" their case and force them to work "unofficially," even if an actual law enforcement person watching the show knows there wasn't a good excuse given to federalize the case there.
The one thing I found out, Not even the police agreed totally on who's in charge of what??