The General Assembly of the Church of Scotland rejected an attempt to block the appointment of an openly gay minister.
The Kirk's ruling body voted by 326 to 267 in support of the Rev Scott Rennie after more than four hours of debate.
Mr Rennie, 37, had the support of the majority of his congregation and presbytery at Queen's Cross Church in Aberdeen - but the move to appoint him sparked strong protest from traditionalist members.
The Kirk would not comment on the decision, other than to confirm the outcome of the vote.
About 900 elders and ministers took part in the session, which got under way at 6.30pm yesterday evening, but not everyone cast their electronic vote.
Demonstrators from both sides had gathered earlier outside the Kirk building, on The Mound in Edinburgh, peaking at about 60 people during the day.
There has been concern that the vote could cause the greatest divide in the Church since the Disruption of 1843, when part of the Kirk broke away to form the Free Kirk.
Before last night's hearing, Mr Rennie played down concerns over the controversy.
``The same talk was about when women were ordained and I think that argument suits those that don't want any change,'' he told Sky News.
He said there are ``many'' gay ministers in the Church and rejected claims that his sexuality contradicts bible teachings.
``We don't stone women, we don't stone adulterers, we've moved on from that,'' he said.
Mr Rennie, who was married, is now in a relationship with another man. He was appointed minister of Brechin Cathedral, in Angus, 10 years ago.
Papers lodged with the Assembly to oppose the appointment claimed that scriptures in the Old and New Testament describe same-sex activity as a ``wrong choice''.
Mr Rennie's supporters argued that the Bible does not directly address homosexual relationships, which are now ``essentially a feature of modern society''.
A group of demonstrators, led by Pastor Jack Bell of Zion Baptist Church in Glasgow, had hoped the appointment would be blocked.
He said: ``We are absolutely opposed to that on the basis of what God has to say about homosexuality in the Bible.''
Reverend Lindsay Biddle of Affirmation Scotland, a group which supports gay and lesbian clergy, said supporters were praying that Mr Scott would be allowed into the church.
``There are many gay ministers in the Church of Scotland, there always have been and there will continue to be,'' she said before the vote.
The General Assembly is the supreme court of the church and has the power to make laws and set the agenda for the coming months.
The Equality and Human Rights Commission Scotland said it was ``delighted'' with the result.
Alyson Thomson, head of communications for the commission, said: ``The Church of Scotland General Assembly has tonight set out a clear stall - it is a modern church for a modern Scotland.
``The commission is delighted that the church has, as Scott Rennie requested, taken an honest look at itself over the issue of sexuality and decided that the values of fairness, equality, dignity and respect are of more worth than those of ignorance and intolerance.
``We are certain that this decision will be welcomed by the majority of Scots and certainly the majority of Queen's Cross parish in Aberdeen who overwhelmingly demonstrated their support for Mr Rennie.''