A guild is not, as many people are apt to say, a kind of 'trade union.' Trade unions were founded as a means of representing a laboring working class against an ownership-management class, to force the latter to make concessions in wages and working conditions.
Guilds are made up of small owner-operators who join together in organizations in order to monopolize a certain product to impose price-fixing upon the consumer. More than a trade union, a guild is much closer to a lobby - in that the larger the number of members, the more influence it has upon the government, encouraging public spending which benefits the guild.
Future members of the guild begin as apprentices, who are educated to practice shoemaking, baking or tanning according to traditional, accepted guidelines already adhered to by the members of the guild. Innovation is not rewarded. Guilds have no interest in competition, personal achievement or the transfer of knowledge. Recipes and methodologies are kept in the strictest of confidence, and are never, ever shared with anyone. Those inside the guild are already perfectly aware, so mention even between members is discouraged, since a casual conversation might be heard by anyone.
Given this sort of conspiratorial attitude among guild members, even those creating the most mundane of material objects, what was the benefit to society? Obviously, the guild benefits, since it controls the trade in that particular guild - but why was the monopoly not challenged? Why did independent breadmakers not simply undersell the competition?
Guilds offered something which competition could not: certified quality of goods. The guild was not founded upon profit, but upon control of trade ... and it relied heavily upon popular support for its products. A failed shoe or an unworthy bread did not encourage people to seek their shoes and bread elsewhere, but to descend upon the shopkeepers with club and spade. It was not a forgiving society. But a guarantee of quality also guaranteed popular support for those guilds operating in a town. Thus was the guild's control and power guaranteed.
And while technological innovation was suppressed, it did occur ... very, very slowly. To the tune of centuries. And since the innovation did tend to result directly from the activities of those within the guild, the innovations were also closely guarded. An independent shoemaker would be likely to produce shoes which could not hope to compete in quality with the guilds ... and since the guilds also tended to control things like raw material goods, a shoemaker that did not own his own cows, and perform his own tanning, was not likely to find those things provided. Thus the self-made leather was a poorer quality, the craftsmanship was a poorer quality and the population had already learned to rely on the street of the cobbler's for their goods. The best an independent shoemaker could hope to do would be to reside in some small backwater where the Cobbler's Guild had no interest.
Thus was born a hundred fairy tales.
The demise of the guild came about when the quality of a product could be reasonably duplicated (not perfectly, mind, but humans are not perfect) by machines, who could produce goods in such quantities that guilds could not compete. Guilds operate under conditions where only so many teapots can be produced by a given silversmith in a day - the limited number of teapots existing in the world then justifies the fixed price for a teapot, and a guaranteed wage for the guild member so long as they continue to sit at their bench and tap away. Moreover, a silversmith can only take on so many apprentices at a time, or is willing to take on an apprentice (if the local market wants one), as it takes considerable time to teach an apprentice to perform the skills of a silversmith to an adequate degree. It can take years for a guild to effectively increase the availability of a product, should the population suddenly swell - which can bring in foreign ex-guild members of other cities, challenging the monopoly. Violence did ensue. And meanwhile, a population was want to do without while the matter was settled.
However, the creation of a factory to make teapots was something that could be done in a matter of months ... and its creation did not require any special education for most of the laborers involved in the factory's creation. One silversmith is consulted in the making of the machine that churns out teapots, and then that machine can be duplicated again and again, by other machines that required the consultation of only one tool-maker.
Quality declined (and artistry, too), of course, and continues to decline, from the level of profound craftsmanship that had been managed by the guilds circa 1700 ... but that doesn't change that in the present day we would rather have an immediate availability for less cost, however quality is compromised, and however little artistry goes into our daily material purchases.
This, I hope, goes to show in some small part why present-day assumptions about economics simply don't apply to the Medieval template. But let's move onto the guild's application in D&D.
In large cities, guilds tended to conglomerate in certain squares or along certain streets. A village of less than 500 people might have only one baker, but a huge city like Venice or Genoa would have scores ... and the baking houses would establish themselves near those places where rurals appeared with their sacks full of milled flour. The mills tended to be in the hills where they could count on running water or wind for power. Moreover, a substantial clean water source would be needed by the bakers as well, plus coal or wood for their ovens ... so the placement of the baker's guild within the city wouldn't be something random.
Because the guild would have money, and their own interests, it was common for them to hire a private police force - what D&D calls the town watch. But the watch would not be worried about the 'town,' but specifically about the homes and businesses of the guild who had hired them. And their duties would be more than to stop thieves. They would actively take part in opposing anyone who could threaten the bakers business, pushing off the unwanted and having disputes with other watches hired by other guilds, whose presence there might also depend upon the farmer's market and abundant fuel.
If the guild was strong enough in local politics, the watch might have unchallenged authority on that street, to break the legs of thieves or even surreptitiously kill strangers in a secluded basement ... with the city's guard looking the other way, refusing to answer complaints or otherwise being impotent to take action.
A strong city might have a hundred little political entities, each one operating on each street, waging private wars, carrying forth vendettas, having territorial aspirations or struggling against being pushed out of the best streets and into the seedier parts of town. A particular guild might be rousting out the poorer inhabitants in the surrounding streets and be engaged in a project of housebuilding for wealthier clients ... or interested in taking advantage of many refugees in a vox populi putsch to put someone of their own guild into political power - if this should happen to be a republican city like Florence or Amsterdam. There are possibilities for adventure, for or against an evil guild, whether they be brewers or puddlers or glassblowers.
It's a shame that in most D&D campaigns the only existing guilds that rise above the radar are the thieves guild and the assassin's guild ... neither of which ever existed in history. Even the Court of Miracles from Victor Hugo's the Hunchback of Notre Dame, or the street of Madame DeFarge from Dickens' Tale of Two Cities were made up of unwanted persons from a variety of different professions and guilds. In truth, the thieves 'guild' is a sort of joke, the way that the oldest 'profession' used to be (though the application of that word is being taken seriously now). Not really a guild at all, but a group of outcasts who have nothing but each other, and enough desperate will to make themselves dangerous.
Fagin's boys (Dickens again, Oliver Twist), of course, were not old enough to have any profession, except that of pickpockets ... but if you can take from that book that pickpocketing was a long-term career path, you might want to read it again.