Old English denn "wild animal's lair, hollow place in the earth used by an animal for concealment, shelter, and security," from Proto-Germanic *danjan (source also of Middle Low German denne "lowland, wooded vale, den," Old English denu "valley," Old Frisian dene "down," Old High German tenni, German tenne "threshing floor," perhaps from a PIE word meaning "low ground").
Transferred to den-like places in human habitations in Middle English: "secret lurking place of thieves, place of retreat" (late 13c.); "apartment, private chamber" (late 14c.), but the modern use for "small room or lodging in which a man can seclude himself for work or leisure" [OED] is a modern development, originally colloquial, attested by 1771. By 1956, however, at least in U.S., the den had come to be a sort of family all-purpose room. In 19c. it also often had a bad sense, "a haunt, squalid place of retreat" (as in the set phrase den of iniquity for a brothel, etc.).
For, in truth, without a den or place of refuge, a man can achieve neither tranquility nor greatness. Jefferson wrote the Declaration of Independence in a quiet rooming house in Philadelphia. The soaring ideas that went into it evolved during hours of reading and contemplation in a secluded library. Had it been a study-TV-guest-family room, the United States might still be a colony. [The Kiplinger Magazine, September 1956]
in good den, found in the early dramatists, a contraction of good e'en "good evening;" the phrase was short for God give you good den.