Forty-six MPs voted for the historic bill and 44 voted against it yesterday, after a two-day debate.
The bill, introduced by independent Sydney MP Clover Moore, allows same-sex couples to adopt a child together. Gay couples can already adopt in the ACT and Western Australia.
Voting on the issue in NSW was always going to be close and the result was initially unclear as a similar number of MPs filled each side of the parliamentary floor.
At the last minute, the doors were unlocked for the dissenting MP and leader of the Nationals Andrew Stoner, who was late because of a problem with the lift.
Reintroducing the bill on Wednesday, Ms Moore included an amendment that gives church adoption agencies the right to refuse services to same-sex couples without breaching anti-discrimination laws.
Premier Kristina Keneally, Opposition Leader Barry O'Farrell and Mr Stoner had allowed the MPs a conscience vote on the contentious issue.
Ms Keneally and Mr O'Farrell both supported the bill, while Mr Stoner did not back it.
The issue split the Liberal Party and the Nationals, with senior members of the Coalition voting against it.
Labor MPs were also divided, with Noreen Hay and Virginia Judge among those opposed.
Those supporting the bill argued it would give recognition to the estimated 1300 children in NSW already fostered by same-sex couples or cared for by a parent's gay partner.
The bill would not affect inter-country adoptions since none of the countries Australia has agreements with allows such adoptions.
The bill would most often apply to situations where a couple adopted a foster child, or a parent adopts the child of his/her partner.
Speaking to the lower house on Wednesday night, Ms Keneally said she had considered her Catholic faith, observations of same-sex parents and her own experiences of parental love.
"In considering all of that, I must, in my conscience, support this legislation," she said.
It's a few days old, but definitely worth posting. The Australian Christian Lobby is crying about it, naturally. A pretty big step forward, especially considering Sarah Hanson-Young of the Greens has said she'll be reintroducing her gay marriage bill into federal Parliament when it sits in a few weeks. This might help the debate. Not happy about the allowance for church adoption agencies to refuse gay couples though.