There are two basic ways. During Shakespeare's life, anyone who could get a handwritten copy of a play could print it. There were no copyright laws at the time, and, since Shakespeare was a popular playwright, anyone who could get a copy of, say, a script used to prompt the actors during a show could print it in order to make money. These printed copies of plays were called quartos because the sheets of printing paper were folded four times. Eighteen of Shakespeare's plays exist in one or more quarto versions.
The other way the plays have come down to us is what is called the First Folio (published in 1623). After Shakespeare's death in 1616, two of Shakespeare's fellow actors printed 36 of the 38 plays attributed to the Bard in a large book. Some plays (such as The Winter's Tale) only exist in the First Folio. Others, like Hamlet or King Lear, exist in both the First Folio and in one or more quarto versions. Sometimes the differences between the versions are relatively minor and sometimes they are very significant. King Lear is a good example of the latter; some editions of Shakespeare's complete works print two versions of this play because the First Quarto is so different from others.
So, when modern editors want to publish a copy of one of Shakespeare's plays, they have to consult all of the versions out there and decide what they want to do about any differences. In addition, they have to make decisions about punctuation, confusing words, and problems like that. Unfortunately, with the exception of one page of writing, as far as we know, none of the plays in Shakespeare's own writing have survived. Gladly, we do have the quartos and the First Folio.