There are several reasons.
Recognizing the urgent danger that now exists that an increase in the number of States possessing nuclear weapons may occur, aggravating international tension and the difficulty of maintaining world peace, and thus rendering more difficult the attainment of general disarmament ... Calls upon all Governments to make every effort to achieve permanent agreement on the prevention of the wider dissemination of nuclear weapons.
-- United Nations General Assembly Resolution 1576 (XV)
First, because non-proliferation was seen as a positive step towards nuclear disarmament. Nuclear war does not just affect involved combatant; the nuclear fallout from a sizeable exchange would be a global ecological disaster. At the height of the Cold War, the threat of nuclear holocaust was a very real concern. This meant most smaller states had a compelling interest in stemming/reversing the the tide of nuclear arming.
The least advanced countries of the Third World, which were in favour of the aim pursued,
hoped that [the nuclear powers] would provide them with genuine assistance for advancement in the domain of the peaceful applications of nuclear energy.
The industrialized countries ... were basically afraid of being handicapped in world nuclear competition vis-a-vis the powers which had entirely maintained their freedom of action and were likely to benefit from the impact of their military operations on the civil domain. They were also anxious to gain from the peaceful applications of nuclear explosions.
-- Goldschmidt, Bertrand. "The Negotiation of the Non-Proliferation Treaty (NPT)." IAEA Bulletin 22.3/4 (1980): 77.
Second, because those countries was not really losing anything from it. Keep in mind that most of the world's countries are not Great Powers. While the terms of the treaty can be seen as unfair, it was not in truth a real disadvantage for these smaller nations. Most of them, and some Great Powers, did not have the political will or technical expertise to develop nuclear arms in the first place (West Germany being a notable exception). Nuclear arms are not a major concern for a country trying to feed its population and raise their economy above agriculture. Such developing nations had nothing to lose by renunciating nuclear weapons; but they could gain (even if meagre) security assurances from doing so.
Nothing in this Treaty shall be interpreted as affecting the inalienable right of all the Parties to the Treaty to develop research, production and use of nuclear energy for peaceful purposes without discrimination ... All the Parties to the Treaty undertake to facilitate, and have the right to participate in the fullest possible exchange of equipment, materials and scientific and technological information for the peaceful uses of nuclear energy.
-- Article IV, Nuclear Non-Proliferation Treaty
Thirdly, and going hand in hand with the second point, because many countries felt they had something to gain. Most of them sought civilian/economic benefits from nuclear technology. During the negotiations for the NPT, these countries - as a condition of their acceptance - extracted concessions in this regard. The security concerns of the non-nuclear states also resulted in promises by nuclear states that they would not use them on non-nuclear states.
Although such a provision failed to be formally incorporated into the treaty, the Great Powers did make formal promises to that effect. Britain, the United States, France and the Soviet Union all made some variation of an assurance to not attack non-nuclear states. Although there are loopholes, this helped confirm the growing consensus that nuclear weapons are not regular bombs and shouldn't be used lightly.