It’s in the Constitution, the supreme law of the land, which applies in each of its fifty states.
The second amendment of this text, written in 1791, guarantees it:
« The right of the people to keep and bear arms shall not be infringed. »
It is specified that this allows the possibility of forming militias to defend, if necessary, the security of the state.
Indeed, a short time before, the Americans had a revolution to get rid of the English and founded a democracy.