The Church of Scotland's ruling General Assembly has voted in favour of allowing openly gay men and women to become ministers.
It backed a motion affirming the Church's "current doctrine and practice in relation to human sexuality", but permitting liberal congregations to depart from that approach if they wish to do so.
The decision, made following a lengthy debate on the issue, will come back before next year's General Assembly for further analysis.
The ordination of ministers in same-sex relationships has divided the Church for many years, with the two sides of the debate broadly characterised as traditionalist or the more liberal "revisionist".
The Church had been expected to vote for one of three options today: to reject the future ordination of ministers in same-sex relationships, to treat openly gay ministers in the same way as heterosexual clergy, or to allow congregations to appoint ministers in civil partnerships whilst permitting individual congregations to opt out.
In fact, the Church voted in favour of a last-minute option brought to the table by a former Church irk moderator, the Very Reverend Albert Bogle.
Church commissioners backed his motion to "affirm the Church's historic and current doctrine and practice in relation to human sexuality, (but) nonetheless permit those Kirk Sessions who wish to depart from that doctrine and practice to do so".
Today's historic vote effectively means that the traditionalist position holds, but congregations will be able to choose themselves to have a minister in a civil partnership if they wish to do so.
Such a move will require certain Church regulations to be written and voted on at the 2014 General Assembly.
Mr Bogle's motion was carried with 340 votes in favour in a final ballot on the issue.
The motion to allow congregations to appoint ministers in same-sex relations, with an opt-out option, fell after netting 282 votes.
Putting his ultimately successful proposal forward, Mr Bogle apologised for the last-minute nature of his intervention.
He told the Assembly: "I really didn't mean to ever be here today. I apologise that you have got papers that have come very late to you. I apologise too because I never meant to enter into this debate, because I was the moderator until very recently."
Mr Bogle, who said he approached the issue from a traditionalist standpoint, said it seemed there were two "polarised views" on the issue, with no obvious way of moving forward.
He told commissioners: "Please will you hear my plea because I speak for people in this room for whom it is even painful to talk of these things?
"We might be wrong and therefore we need to be gracious and listen to others and be prepared to move a little.
"My motion is that that by seeking to maintain what I believe the Church of Scotland should be ... (we) recognise that there are others who perhaps have greater enlightenment in the power of the spirit and are much braver than I could ever be and say, 'this is the way we should be going'.
"I don't want to be the one who stands and says, 'you must not do this', because I have worked and lived in this Church with many of them for the last X number of years and we have loved one another.
"My motion is to be permissive and to allow those who want to do this to do it. But I want to affirm the position of the Church of Scotland in the historic tradition of the Church."
He added: "I ask that you take that step with me. It will give everyone what they want but it will keep us together. It will not be what everybody desires because we will all in some way be hurting, but we'll have moved a little bit and we'll have confounded the critics that we cannot stay together as God's people."
Seconding the motion, Rev Alan Hamilton of Bearsden Killermont parish, voiced reluctance to move from a traditionalist view, but said it was time to make some concessions.
"I do not want to depart from the traditional view of the Church, a view I believe is enshrined in the Bible and the will of God," he said.
"But I believe that this is the time for the Church, particularly traditionalists like me, to allow others who disagree space to express that disagreement."
He described it as an option "which does not require the Church to abandon its traditional position and all that flows from it, not least our position among world churches".
"That would be too far, it would be too fast, it would be too painful, it would be too destructive," he added.
Rev Elisabeth Spence, a gay woman based at Glasgow Ibrox, spoke in favour of the unsuccessful motion backing the ordination of gay ministers, with a conscience clause allowing ministers and congregations to opt out.
She said: "I actually believe we've got bigger things to think about. Society has got bigger things to think about, the world needs us to do bigger things.
"However, for me personally, this debate is about me. It's about me as a woman who serves a church of people for the last 18 years, a congregation for the last five years, who are nothing but supportive and loving.
"For me there is nothing bigger about whether I am accepted in this Church or not. From 1994 it's been debated, am I accepted in this Church or not because I am a gay woman?
"It's now time, it's time to decide, so that those of us who are in this limbo can get under the wire."
The motion voted on today will come back before the General Assembly next year before it is likely to be rubber-stamped by the Assembly in 2015.
However Rev David Randall, of Macduff, said the issue of gay clergy "has been forced upon us by the revisionists who want us to turn our backs on what common sense tells us".
He said: "If we go revisionist or try to sit on the fence then we will lose members, ministers, congregations and money.
"Are we to stand by scripture or are we to go with the flow of social trends?"
Equality Network, Scotland's lesbian, gay, bisexual and transgender (LGBT) equality charity, welcomed the move, saying it was a positive step forward towards a more equal society.
Policy co-ordinator Tom French said: "We welcome this decision by the Church of Scotland, which is particularly important for the many LGBT people within the Church and their friends and family. This is a positive step forward for a more equal society, and speaks to the progressive values of 21st century Scotland."
But the Free Church of Scotland branded the outcome of the debate "totally confusing".
A spokesman said: "The winning motion says that it affirms the historic doctrine of the Church, which is of course opposed to the ordination of ministers in same-sex relationships.
"Yet at the same time the motion permits congregations to call ministers in same-sex relationships. The whole thing is totally confusing.
"We don't understand what's going on in the Church of Scotland, and suspect the vast majority of the Scottish public don't have a Scooby either.
"We believe that Scotland needs the guidance of the national church rooted in the teachings of the Bible, irrespective of public opinion and pressure to conform.
"Today's debate simply demonstrates the growing confusion amongst Scotland's national church, at a time when the public are looking to it for direction from the scriptures."