There are two different theories for the formation of Freemasonry:
The first theory is that operative stonemasons who built the great cathedrals and castles, had Lodges in which they discussed trade affairs.
They had simple Initiations for Apprentices and Fellows and, as there were no City and Guilds certificates, dues cards or trade union membership cards, they adopted confidential signs and words so that they could demonstrate they were trained masons when they moved from site to site. In the 1600s these operative Lodges began to accept non-operatives as 'gentlemen masons' who gradually changed these lodges into 'free' or 'accepted' Lodges.
The second theory is that the group who formed Freemasonry (in the late 1500s and early 1600s) were a group who were interested in the promotion of religious and political tolerance in an age of great intolerance, when differences of opinion on matters of religion and politics were to lead to bloody civil war. What they were trying to do was to make better men and build a better world. As the means of teaching in those days was by allegory and symbolism, they took the idea of building and construction as the central allegory on which to form their system. The main source of allegory was the Bible, the contents of which were known to everyone (even if they could not read), and the only building described in detail in the Bible was King Solomon's Temple, which became the basis of the ritual. The old trade guilds provided them with their basic administration of a Master, Wardens, Treasurer and Secretary, and the operative mason's tools provided them with a wealth of symbols to moralise upon.
It is quite possible that the origins of Freemasonry, as we know it today, are a mixture of both of these theories. The first record of the 'making' of an English Freemason is Elias Ashmole, the antiquarian and herald, whose collections formed the basis of the Ashmolean Museum in Oxford. He recorded in his diary that a Lodge met at his father-in-law's house in Warrington, Cheshire on 16 October 1646 to make him a Mason. None of those involved was a stonemason. In the later 1600s there is further evidence for the existence of Freemasonry as a separate organisation unrelated to groups controlling the stonemason's craft.