Old English be- (unstressed) or bi (stressed) "near, in, by, during, about," from Proto-Germanic *bi "around, about" (cf. Old Saxon and Old Frisian bi "by near," Middle Dutch bie, Dutch bij, German bei "by, at, near," Gothic bi "about"), from *umbi (cognate with second element in PIE *ambhi "around," cf. Sanskrit abhi "toward, to," Greek amphi- "around, about;" see ambi-).
Originally an adverbial particle of place, in which sense it is retained in place names (Whitby, Grimsby, etc.). Elliptical use for "secondary course" (opposed to main; e.g. byway, also cf. by-blow "illegitimate child," 1590s) was in Old English. This also is the sense of the second by in the phrase by the by (1610s). By the way literally means "in passing by" (mid-14c.); used figuratively to introduce a tangential observation by 1540s.
Phrase by and by (early 14c.) originally meant "one by one," modern sense is from 1520s. By and large (1660s) originally was nautical, "sailing to the wind and off it," hence "in one direction then another."