The Ramchal in Da'as Tevunos in the section בריאת הרע וגדריו, starting with siman 96 and particularly in siman 114 and 118, explains how demons came into existence.
114:
כשאנו אומרים שהקב"ה ברא העולם הזה, ודאי נבין בתחילה בריאת הכלל, ואחר כך הפרטים, פירוש, בתחילה הטבע עצמו, ואחר כך אישיו. והנה כשרצה האדון ב"ה לחדש הטבע בטוב ורע, הנה ודאי הוא שבאה השפעה ממנו לחדש מציאות הטבע, וזהו הטוב; וזאת ההשפעת עצמה - קצת ממנה נעדרה, היינו, ההכנות הטובות שהיו בה, ותנאי השלמות שלה שהיו עושים הטבע בתכונתו טוב. והנה מציאות ההעדר הזה הוא שחידש בטבע הפסדי המציאות הטוב, שהם הם כלל הרע.
118:
אך בני האדם הם פחותים מן המלאכים וחסרים מהם, והחסרון בהם גבר כל כך שיש בהם מה שהוא רע, כי יש בהם יצר הרע, חולאים ומיתה. בעלי החיים יש בהם חסרון יותר, שאין להם לא שכל ולא דיבור, והם מזוהמים ומטונפים. ויש המזיקים ומלאכי חבלה ורוחות הטומאה שהם רע ממש, והם ההפך ממש לטוב ולשלמות, אותם שהשם העליון ב"ה מתעלם מהם העלם גמור
They are, in short, a natural outcome of the degeneration of the world into evil. It is hard to sum up, but the basic idea is as follows: the world was created in two stages, first with a general construct of all the structure of creation, and then the specific entities that were created, both spiritual and physical. The original construct defines what exists, and the subsequent "supply" gives the degree of perfection of that existence. When Hashem "hides His presence" as a result of our actions and misdeeds, this construct still exists, but without perfection. When the "hiding" reaches a certain threshold, things that are evil manifest result. That is where demons and destructive forces come from. Their existence is built into the framework of creation to allow "destruction" to exist.