[main body of a tree] Middle English stemme, from Old English stemn, stefn "trunk of a tree or shrub," the part which rises from the ground and supports the branches; also "either end-post of a ship;" from Proto-Germanic *stamniz (source also of Old Saxon stamm, Old Norse stafn "stem of a ship;" Danish stamme, Swedish stam "trunk of a tree;" Old High German stam, German Stamm). This is thought to be from a suffixed form of the PIE root *sta- "to stand, make or be firm."
Especially of the post at the bow of a ship, hence the word came to mean "front of a ship" generally by 1550s. That sense is preserved in the phrase stem to stern, which is originally nautical, "along the full length" (of a ship), and is attested from 1620s.
By 1590s as "stalk which supports the flower of a plant;" the meaning "support of a wineglass" is by 1835. Extended to other things resembling the stem of a plant; in type-founding, "thick stroke of a letter" (1670s); stems as slang for "legs" is by 1860.
The sense of "stock of a family, ancestry" is attested by 1530s; the sense also is in Middle High German stam, Dutch stam; Old High German stam is only in the literal sense, but it meant "race" in compound liut-stam; for which also compare Old English leodstefn "race."
In modern linguistics, the sense of "part of a word that remains unchanged through inflection" is from 1830. In biology, stem cell is attested by 1885.