Following years of debate between the liberal and traditionalist sides of the church, the Church of Scotland voted to allow gay ministers.
Scottish gay rights campaigners praised the move but the Free Church of Scotland called it a fudge.
The General Assembly pledged commitment to its "current doctrine and practice in relation to human sexuality" but said it would not prevent liberal congregations to opt out of the convention.
Church leaders were expected to vote for one of three options: to reject the ordination of gay men and women as ministers, to allow their ordination or to allow their ordination but with the option for individual congregations to opt out.
In the end, the Church voted for a fourth proposal put forward by the Very Reverend Albert Bogle.
Bogle's 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" was carried with 340 votes in favour to 282 against.
"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," said Equality Network policy co-ordinator Tom French.
"This is a positive step forward for a more equal society, and speaks to the progressive values of 21st century Scotland."
However, the Free Church of Scotland, which split from the Church of Scotland in the 19th century, called the result "totally confusing".
"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," said a spokesman.
"Yet at the same time the motion permits congregations to call ministers in same-sex relationships. The whole thing is totally confusing."