There are many other terms and expressions and relating to "way" for example, sidetrack, out of the way, roundabout way of speaking, etc. Where did the term by the way come from? I've googled it, to no avail, and may be its because it's been coined in a book or something. Does anyone know anything relating to this saying? And maybe explain the popularity of terms relating to "way".
Your question is conflating two different meanings of "way".
"By the way" is literally "by the side of the road", but the OED cites its figurative use ("Incidentally, in passing, as a side-topic.") from 1556. I don't believe a single authoritative source will ever be identified for it, as I guess it was in common speech before 1556.
"Sidetrack" uses the same metaphor, as you imply.
But "roundabout way of speaking" is "roundabout [way of speaking]", and uses "way" in the sense of "Manner in which something is done or takes place; method of performing an action or operation." (the OED's sense 14 a).
The Corpus of Historical American English has examples of sentences containing by the way from 1810 and later years.
That, Sir, is the type, symbol, and adopted emblem of our nation, the BALD EAGLE, who, by the way, is not bald, any more than that stout and sturdy youth, the thriving republic, whose character he represents; only his head and neck are white; so is his tail; the rest of his body is brown. —North American Review: March 1818: 405-409.
It is also used in the following sentences:
But faith I must be off, or day-light will overtake me by the way: Oh good by my poor dear, dead girl, I suppose your ghost will be after following me over the country, and never cease crying, Dermot, you kilt me. exit.—M. (Mary) Clark, The Fair Americans (1815).
He comes by the way of the fields, creeping through hedges, leaping ditches, and looking around him with all the apprehension of a felon that has just broke jail.—William Dunlap, The Italian Father (1810).