The Church of Scotland has voted to allow the ordination of openly gay ministers - after an emotional debate and pleas by traditionalists from all over the country to reject the move.

The decision was made yesterday by the General Assembly on the Mound in Edinburgh after more than two hours of debate, with the motion passed by 309 votes in favour and 182 against.

Following years of deliberations over the issue, the Church has now adopted a position which maintains a traditional view of marriage between a man and woman - but allows individual congregations to "opt out" if they wish to appoint a minister or a deacon in a same sex civil partnership.

The ordination of ministers in same-sex relationships has divided the Church since traditionalist members attempted to block the appointment of Rev Scott Rennie - who is in a civil partnership - in Aberdeen in 2009. A total of 21 out of 806 ministers subsequently quit the church over the issue.

Yesterday Rennie said it was a "great outcome for an open, broad and faithful Church of Scotland."

But others were not so welcoming of the move, with dire warnings issued that it would lead to "gay ghettos" and that John Knox - the father of Scots Presbyterianism - would be "turning in his grave". Opponents also suggested the move could still be open to legal action.

However Hannah Goodlad, a youth representative, said: "We have a responsibility and duty to move this Kirk forward or 10 steps backward. At the end of the day is the calling of a gay minister less valid than the calling of a straight minister?"

Also at the assembly, Stefanie Fowler, of Aberdeen, said: "By being culturally irrelevant you are not going to attract young people.

"I know young people who are called to the ministry who have changed their minds because they cannot represent a church that holds those beliefs."

Rev Jerome O'Brien, of Dollar, Glendevon and Muckhart, Falkirk Presbytery, said the move would mean "for the first time in eight years we are finally reaching consensus."

However Rev Gordon Kennedy, of Craiglockhart in Edinburgh, claimed allowing congregations to choose gay ministers would lead to "ghettos".

Rev Thomas MacNeil, from Martin's Memorial in the traditionalist enclave of Stornoway, claimed John Knox would "turn in his grave" over the debate.

And Rev Mike Goss, of the Covenant Fellowship which opposes gay clergy, said it would continue to challenge the stance. He said the move could be open to legal action and added: "It is not anything like the end."

Because the debate predates the introduction of gay marriage, the proposed change mentions only civil partnerships, not same-sex marriages.

The issue will be followed up on Thursday when the assembly will be asked to include ministers in same-sex marriages in wider Church law.

Moderator Angus Morrison called for "moments of reflection" after the debate.

And outgoing Moderator Very Rev John Chalmers said: "There's something else that we have to learn as a Church and that is the power of harmony.

"Of course we need the freedom across the Church to shape the life and worship of the Church according to local needs and local gifts but we cannot go on suffering the pain of internal attacks which are designed to undermine the work or the place of others. It's time to play for the team."

The General Assembly, the annual gathering of the Church of Scotland, will run until Friday, with topics ranging from tax reform and food banks to fracking and nuclear weapons to be debated.