Multiple theories have tried to explain the origin of the fraternity. Most center around an aristocratic adoption of the language surrounding stone-working guilds as a method of allegorical teaching. More fanciful theories posit the origin of the organization in the Knights Templar. What is the truth to the best of the knowledge of the historians here?
There are multiple theories, but most historians nowadays subscribe to the Scotland thesis. The basic idea is that Freemasonry originated in the medieval masonry guilds of Britain and France, but after the English Civil War some masonry guilds began to allow non-masons into their lodges and the club began to become quite popular amongst freethinkers, dissenters and gentlemen of the period. Freemasonry taught lessons of self-improvement, equality and enlightenment through symbols and was more than just a club for some but a place to discuss esoteric topics. Anyhow, according to David Stevenson, The Origins of Freemasonry:Scotland's Century, 1590-1710 (Cambridge, 1988), the first modern lodges appeared in Scotland around the turn of the 17th century when William Schaw laid down the ritual and began organizing new chapters that focused more on the ceremony than the traditions of the guilds. The formation of lodges in London that would unite to form the United Grand Lodge of England would only occur later in 1717 (although they claim to be the "mother" lodge and the founders of the fraternity, this is heavily disputed).
But if you're interested, there is plenty of debate on the topic. Aside from the Stevenson book and the Scotland thesis (which is now considered by most to be the true origin of Modern Freemasonry), see Margaret C. Jacobs, The Origins of Freemasonry (Philadelphia: University of Pennsylvania Press, 2007). For a review of the debate, see Steven C. Bullock, “Initiating the Enlightenment?: Recent Scholarship on European Freemasonry” Eighteenth-Century Life 20, no.1 (1996).
So from the fraternities own mouth, the masons attribute their origin to the workers, apprentices, fellow crafts and masters who built King Solomons temple. It was at that point in biblical history that many rules of geometry and arithmetic were learned. Sometime between then and now what is known as Speculative Masonry was born, where the rules and equations were given more philosophical meanings. For example the 24 inch gauge was a tool for measurement that was later given a speculative meaning that your time, in a 24 hour day, should be divided evenly in three 8 hour segments, for work, refreshment, and worship.
Where this exact moment occurred, the divide between operative and speculative, is extremely hard to pinpoint. One of this highest rules of masonry is that the teachings and lesson are not to be carved, etched or in any way marked for the uninitiated to read. Therefore many records books and archives were meaningless and easily lost to time.
The knights templar are connected to masonry in both physical connection, which I will not digress into for this particular question, and out of speculation. Historians in the fraternity speculate on many knights being members of ancient Masonic organizations. If you'd like more information on this subject I'd be happy to try to source some reading material up that can better explain it.
The origin of FreeMasons as you see them today comes from a series of lodges in the late 1600's to early 1700's having disputes about ritual work(how the teachings are taught and bestowed) and the rules that should govern conduct, and the meaning behind the teachings all together.
In 1723 the first United Grand Lodge of England was convened to handle the dispute. Until this time there was no governing body, aiding to the amount of theories of origin. This is where they formally consecrated Freemasonry as an organization. An official document serves as proof of such a meeting, known as the Anderson Constitution.