system
Given the following post, respond as if you're commenting on Reddit:
human
ELI5: In theory, how do all of the US government armed forces (Police, FBI, DEA, etc) rank and compare to each other?

Just walking around Penn Station, NY today, i saw..

- Over 5 army fatigue-clad guys standing around holding gigantic assault rifles / machine guns..
- Equally heavily armed "Homeland Security" police
- Standard NYPD guys just walking around

This just got me wondering. There are so many different authorities out there, I'm wondering how they rank? Off the top of my head, I can think of:

- nsa agents
- secret service agents
- cia agents
- fbi agents
- homeland security
- Military Police
- DEA agents
- US Marshals
- State Police
- Local Police

I'm going to make up a scenario. 

Lets say you had 2 armed officers from each of these categories, standing around at a government banquet in the middle of a place where you can't call back-up very easily. Suddenly, there's a crisis situation! Maybe terrorists attacking, Die Hard style, or something.

Who instantly has like the most authority? Does it even work that way? How many of them could arrest a civilian if they needed to? If they needed to work together, would one of them 'officially' have the most authority to coordinate them? 

Is there any kind of heiarchy to all of this? I'd really love to know who has the most sway, the most power, and in any kind of emergency situation, who ranks above who else.

EDIT: I'm specifically referring to the ARMED AGENTS for these different forces. For example, I've read the NSA/CIA (etc) all have armed personnel who work specifically for them.
gpt
They all have different responsibilities.   The Federal level is above the State.  And the State level is above the Local.  State level in most states acts as a sheriffs department which is just like your local police, but across the entire state and used often for rural areas that do not have a local police force.

In the case of a terrorist attack, the FBI, the CIA, the NSA, and Homeland Security would all work on the locating the perpetrators, figuring out how they did it, and if there is any other imminent threats.

The CIA would most likely work on gathering intelligence about the chain of command across the world, taking them to wherever the idea of the attack developed.  The NSA would start gathering intelligence domestically through wiretaps.  The FBI would do more of the foot work in investigating the actual scene and probably do some intelligence gathering of their own.  Homeland Security would start investigating who the people were by checking immigration records and other international criminal activities associated with the event.  Homeland Security also controls the TSA at the airport, so they would be responsible for figuring out how the terrorists were able to avoid any detection at the airport when first coming to the US and if they were on any warning lists, like the do not fly list.  

For example, if a murder happened in a city that has a local police force they will be the first to the scene.  If the crime involves a larger matter, like a series of murders that goes across state boundaries, then the FBI would take over.  The more complex and spread out the crime the more likely the local police would hand off the case to the FBI.  The local police usually remains to provide local assistance and lower level investigation (door to door questions, local probes, etc).  

**Intelligence -**  There are 16 intelligence elements in the United States that are supposed to work alone and together.  The over-arching umbrella over these 16 elements is led by the Director of National Intelligence.

NSA and CIA.  The NSA is under the Department of Defense.  The CIA is an independent agency, but they are all suppose to report to the President with daily briefings and to work together, especially after 9/11.


**Department of Justice -**

Federal Investigations:  FBI

Drugs:  DEA

US Marshalls: More or less the police for the Courts. They handle warrants, transporting prisoners, track fugitives

**Homeland Security -**  Responsible for protecting the citizenry from a terrorist attack before it happens

Secret Service:  Protects the current and past president's and their families, as well as foreign dignitaries.  They are also responsible for preventing the counterfeiting of US currency and US treasury bonds notes.


Many others:  TSA, Immigration and Customs Enforcement (ICE), Citizenship and Immigration Services, Customs and Border Protection.

Edit:  Changed state level police per the comments below.