By and by is usually an adverbial phrase meaning (1) after a while, or (2) soon—for example, “I can’t come now, but if you wait a little while, I will be there by and by.” But it also works as a noun meaning the future—for example, “I will see you in the by-and-by.” In the noun sense, the phrase is often hyphenated for clarity. By the by means incidentally, by the way, or beside the point.
Both might be considered out of place in formal writing. Bye and bye, bye the bye, by the bye, etc. are misspellings.
By and by is the older of the two. Its original sense was one by one. Chaucer, for one, used it in this sense in the late 14th century.1 It developed its modern senses by the 16th century and appeared in Shakespeare, the King James Bible, and many other well-known texts of that era. By the by is a couple of centuries newer. Originally upon the by or on the by, it took its modern form by the 18th century and was fairly common by the 19th century.2
Both phrases have declined since their peaks in the 19th century. To modern English speakers, they tend to have an old-fashioned tone, and by and by in particular has a biblical ring because of its use in English translations of the Bible and in Christian hymns.
These writers use the adverb by and by in its modern sense:
By and by, a new home was built with Allen spearheading the efforts as construction manager. [The Huntsville Times]
We shall know, by and by, if she has it in her to handle the challenge. [Deccan Chronicle]
By and by, everyone Sam has anything to do with ends up dead, and he stays alive only by the not-so-good graces of MI6. [Daily Herald]
Here’s an example of the phrase in its rarely used noun sense:
Everybody has so much money that serious negotiations can be deferred into the by-and-by. [New York Times]
And the following are examples of by the by used well:
The song, by the by, is infectious in a get-behind-me-earworm kind of way. [Irish Times]
By the by, why are the doughy guys with big hearts always named Sam in high fantasy? [Houston Chronicle]
Attorney Grieve left the distinct impression, by the by, that he regarded the Press and internet users as impertinent, malign hyenas. [Daily Mail]