Speaking with regards to “law” .... The Nuclear Non-Proliferation Treaty has been signed by the majority of the nations on the planet. That treaty states that the only five nations allowed to possess nuclear weapons are Russia, China, the UK, France, and the US (as the five major winning powers in WW2 and five main powers in the UN). Nations are not bound to sign this treaty however, and many chose not to. For instance India and Pakistan are not signatories of the treaty, and both possess nuclear weapons. Nations like Iran, Iraq (with Saddam), and North Korea are held to the flame because they are signatories of the treaty, yet violate its authority.
Speaking with regards to morally why the US is allowed to have nuclear weapons…. The best answer would be that there is no real reason we should be allowed to have them and others not. But in fairness, in 60–70 years of having them we’ve only used them once, and it was in a major state of war. We’ve proven we’re not going to just pull them out and use them to beat minor foes or use them for any little skirmish that erupts between us and another nation.