“Goody Two-Shoes” was a real person—or at least, a real fictional character. It was the nickname of the title character in a nursery tale called The History of Little Goody Two-Shoes, published anonymously around 1765 by John Newbery. (Newbery is sometimes called “The Father of Children's Literature" because he was the first to make the genre profitable.)
The History of Little Goody Two-Shoes revolves around a poor orphan named Margery Meanwell, whose virtuous farmer father is ruined before his death by a pair of villains called Graspall and Gripe. Margery is so poor she has only one shoe, but a rich relative of the local clergyman takes pity on her and buys her a second one. Margery is so excited by her gift that she runs around exclaiming, "Two shoes, ma'am, two shoes!", or something to that effect, to everyone she meets. In the story, she eventually becomes a schoolteacher and marries a rich man, using her wealth to help the poor. The story was in keeping with the 18th and 19th century taste for gratingly virtuous heroes and heroines in kids’ books and became a huge bestseller, reprinted over and over in various forms.
But the author of the tale—some say it was Newbery himself—wasn't the first to use the phrase “Goody Two Shoes.” As the linguist Michael Quinion notes, it also appears in a 1694 poem by Charles Cotton, "A Voyage to Ireland in Burlesque," as a term for a bad-tempered housewife: “Why, what then, Goody two-shoes, what if it be? / Hold you, if you can, your tittle-tattle, quoth he.”
Though we think of the term today as referring to someone who is smug about being good, Quinion says that implication is only from about the 1930s. Originally, it was more about class. "Goody" was originally a polite form of address for poor married women, a shortening of "goodwife." (The male equivalent was "goodman.") That usage goes back to at least the 1550s, and it’s most likely how people would have thought of the term when The History of Little Goody Two-Shoes was published. For a time, "goody two shoes" was also depreciative term for a lower-class woman or a woman with lower-class tastes and manners (the male equivalent being "goodman two-shoes"). That term may predate the book, or arise from it—it’s not entirely clear which came first.
But the story definitely helped create the idea of a "goody" being someone who is always dutiful and well-behaved. Originally, that wasn’t always a bad thing (see the taste for gratingly virtuous heroines mentioned above). By the 1870s, there was another phrase, "goody goody," based on an early 19th century sense of "goody" as someone “characterized by inept manifestations of good or pious sentiment.” According to Quinion, the idea of the “goody goody” influenced our modern usage of the term “Goody Two Shoes.” When we use the phrase today, that’s the kind of "goody" we’re referring to. But if it wasn’t for little Margery Meanwell, we might not be saying it at all.