The Abortion Legislation Bill has passed its third reading in Parliament, meaning the procedure will now be removed from the Crimes Act.
The bill passed on Wednesday evening 68 votes to 51 - a much narrower margin than at the second reading.
Justice Minister Andrew Little said for more than 40 years abortion had been the only medical procedure considered a crime in New Zealand.
He said change had finally come and safe abortion was legal.
There will no longer be any legal test for having the procedure earlier than 20 weeks, leaving a woman to self-determine whether to have the procedure, with advice from her doctor.
Previously two doctors were required to approve an abortion only if they determined there was "serious danger" to the woman's physical or mental health. For an abortion to occur after 20 weeks, it could only be approved if deemed necessary to save the woman's life or prevent serious injury.
Earlier in the day, an attempt to put the issue to a public referendum failed in a conscience vote during the bill's committee stage by 100 votes to 19 votes.
NZ First had supported the bill, introduced by Justice Minister Andrew Little, on the understanding it would go to a referendum.
The Green Party also failed in its attempt to reverse a blunder that resulted in "safe zones" being excluded from the legislation.
The bill originally proposed 150m safe zones be placed around abortion clinics to prevent protesters harassing people coming and going.
Last week, a voting mix-up resulted in that provision being axed.
Greens' co-leader Marama Davidson on Wednesday requested Parliament go back and reinsert safe zones, but MPs voted 77 to 43 against it.
Under the bill the decision to have an abortion will be made by the woman in consultation with a qualified health practitioner.
Abortions later than 20 weeks will need to meet a statutory test.
National MP Agnes Loheni strongly opposed that provision in her speech to the House.
"Section 11 of this bill by every reading of it states it will allow abortions up to the moment of birth and no amount of the minister blocking his ears and saying 'no it doesn't' changes this," she said.
Amy Adams rejected the arguments against abortion after 20 weeks.
"Women do not wake up late in their pregnancy and have just changed their minds for no reason. Women do not callously throw away a much wanted pregnancy because of a difficult diagnosis," she said.
Adams said she has held the hand and sat with women she knows and loves, who have had to make the incredibly difficult decision to have an abortion.
"I have seen the delays, the difficulties, the struggles, the judgement, the abuse frankly and feel marginalised and criminalised because of our law and that is not okay," she said.
Adams said she was very proud in her last months in the House to put that right.
There was uncertainty about how New Zealand First would vote as their bid to make the bill subject to a referendum was voted down.
In the end it was a female-male split; the two women MPs voted for the bill and the seven men voted against.
Ultimately the ayes won, though with a much narrower margin than at the second reading.
Andrew Little said it was an historic moment.
"The changes to abortion law have been sought for a long time, many governments on both sides of the House have thought about it, no one has done it and with the support of Prime Minister Jacinda Ardern we've done it," he said.
Little said women in New Zealand have campaigned for decades to have abortion treated as a health issue instead of a crime and that change has finally come.