The countries with the ability to create nuclear weapons don't want to use them. They also don't want other countries to use them. So, if a country gains the ability to create nuclear weapons—as North Korea and Iran have done in recent years—the rest of the world will step in and regulate the country's nuclear supply.
The International Atomic Energy Agency is an international agency tasked with the promotion of safe nuclear energy usage, as well as dissuading and preventing foreign entities from using their nuclear energy for military purposes.
In order for countries to have access to the benefits of nuclear power, but not have access to the destruction of nuclear weapons, we need agencies like the IAEA. With experts at the helm and buy-in from countries around the world, regulation is possible.
The Treaty on the Non-Proliferation of Nuclear Weapons is another viable way to regulate nuclear weapons capabilities. The treaty has been signed by 190 countries and attempts to prevent the use and proliferation of nuclear weapons.
The treaty itself is reviewed every five years and changed or updated on an as-needed basis. While the treaty has not been as successful at decreasing the world stockpile of nuclear weapons, it's certainly a start.
Nuclear weapons don't just pop up in countries around the world, and with regulatory commissions and bi-lateral treaties, the global community can stop the development and spread of warheads with exceptional ease.