What did “demon” mean?
No single ancient word lines up with the English word demon, and the closest candidate in the Hebrew Bible began life in Mesopotamia as a protective spirit.
. JJ Johnson.

The short answer is that the word did not mean one thing, and that the closest candidates change meaning depending upon the language, the century, and the community doing the writing. A reader who wants a tidy definition will leave disappointed, although what replaces the tidy definition is considerably more interesting than the definition would have been, because the history of the word contains a reversal that most modern discussions never mention.
The Greek word, which is where our English comes from
Greek offers the most familiar starting point, since the English word descends from daimōn by way of Latin. The entry on demons in the Dictionary of Deities and Demons in the Bible observes that daimōn and daimonion could refer to divine power, to intermediary beings, or to other-than-human agencies more generally, and that those meanings sharpened toward something closer to the Satanic demons of later usage within Jewish, Christian, and magical texts. A daimōn in a classical author is therefore not automatically a hostile spirit. When Plato's Socrates describes the daimonion that warns him away from certain actions, he is not reporting a haunting; he is describing a divine sign, and the word carries no suggestion that the sign is malevolent.
The same DDD entry makes a second point that matters more than the first, which is that many ancient Near Eastern systems were fundamentally unified, however morally complicated those systems were, and lacked both an arch-enemy Devil and a rival demonic kingdom in the later sense. If a reader begins from late dualism, that reader will find demons everywhere in earlier texts, usually by misunderstanding the grammar of the world that produced them.
The Hebrew problem, which is the opposite problem
Hebrew presents the difficulty from the other direction, because the Hebrew Bible does not employ a general, catch-all term equivalent to the English word demon. That absence surprises people, and the absence is routinely misread as evidence that ancient Israel had no category for malign spiritual forces. The category is certainly there; the category is simply expressed through specific figures, functions, poetic personifications, ritual prohibitions, and polemical contrasts rather than through a single convenient label. The data are present and the grammar is not modern, which is a different situation from absence.
The clearest way to see the difference is to follow one word from the oldest attested usage through to an English Bible, because the English Bible is where most readers meet the vocabulary and the English Bible stands at the end of a long chain rather than at the beginning.
A worked example: the šēdîm of Deuteronomy 32
The Song of Moses accuses Israel of having sacrificed unto demons, no-gods, gods that they knew not; and Psalm 106 laments that Israel sacrificed their sons and their daughters unto demons. The Hebrew word in both passages is šēdîm, which is cognate with the Akkadian šēdu. R. Campbell Thompson, writing at the beginning of the twentieth century, concluded that Israel's small demonological vocabulary was largely imported, and his wording is worth having in front of us:

most of the few which they employ are borrowed directly from Babylonia, the Hebrews using šēdîm (i.e., sedu) and lîlît (i.e., lilitu)
Thompson's observation about borrowing has worn reasonably well. His implied equation of borrowing with continuity of meaning has not, and the reason is the reversal mentioned at the start. In Mesopotamia the šēdu was frequently a protective being, paired with the lamassu as a guardian of persons and buildings; the colossal winged bulls that flanked the gates of Assyrian palaces belong to this protective imagery. Whatever a Babylonian scribe meant by šēdu, that scribe was not describing the thing a modern reader pictures when encountering the word demon.

The path the word takes from there is short and almost entirely interpretive. DDD describes the foreign divinities of Deuteronomy 32:17 as beings properly described in the Hebrew text as šēdîm, tutelary spirits, and observes that the Septuagint rendered them as daimonia. So the semantic route runs from a protective spirit in Mesopotamia, to a foreign object of illegitimate worship in Israelite polemic, to a demon in the Greek translation, and finally to the devils of the King James Version, which renders the same line, “They sacrificed unto devils, not to God.” Each step is intelligible on its own terms; no step is neutral. A reader who meets the word devils in an English Bible is encountering the end of that chain rather than its beginning.
Why the dates have to be kept apart
Every claim above depends on keeping three dates distinct, which is the discipline the book applies to every text it handles: the date of composition, the date of the surviving manuscript, and the date of the tradition standing behind the text. The three are regularly different, and a great deal of confident writing about ancient demons depends on quietly collapsing them. The Septuagint translator who chose daimonia was making a decision in Greek-speaking Judaism centuries after the Hebrew line was composed, and the King James translators who chose devils were making another decision roughly two millennia after that. Neither decision tells us what an Israelite writer meant, and neither decision tells us what a Babylonian scribe meant before that.

I would be cautious about pressing the Mesopotamian background further than the evidence allows. That šēdîm is cognate with šēdu is secure; what an eighth or seventh century Israelite audience actually heard in the word is a harder question, and the polemical setting of the Song of Moses gives us a writer with reasons to make foreign objects of worship sound diminished rather than a writer offering a neutral lexical report. The honest position is that the borrowing is demonstrable and the continuity of meaning is not.
So what should a reader do with the word?
Ask which language, which century, and which community, before asking what a demon is. The question “what did demon mean in the ancient world” turns out to be several questions wearing one coat, and separating them is most of the work. A Greek author using daimōn, a Hebrew writer using šēdîm, a Septuagint translator choosing daimonia for that Hebrew word, and a seventeenth-century English translator choosing devils are four different people making four different decisions, and the last of them is the one modern readers inherit. The protective winged bull at an Assyrian gate and the devil of an English Bible stand at opposite ends of the same word, which is a stranger fact than anything the popular version of the story offers.
