A Dungeon Spirit is a small Hardmode flying enemy that can go through walls and drops Ectoplasm. They will not spawn until you have defeated the hardmode Jungle boss, Plantera.
After defeating Plantera, whenever you kill an enemy with more than 100 health points in the hardmode Dungeon there is a 6.67% (1/15) chance of a Dungeon Spirit spawning. Even non-dungeon enemies might spawn one, as the location of the kill is the defining factor, not the monster itself.
On the mobile version, after Plantera's defeat, any enemy, regardless of health, has a 1 in 15 chance of spawning a Dungeon Spirit.
An easy way to farm Dungeon Spirits is to make a mob farm using a Shark Statue or a Chest Statue, as Sharks and Mimics have 300 and 500 max health respectively, making them eligible to spawn a dungeon spirit when in the dungeon. During Halloween the Skeleton Statue will spawn one third of the skeletons with more than 100 HP as well.
Another easy way to farm Dungeon Spirits is to find or place a water candle in a large room that has many tunnels leading into it and use the Piranha Gun on enemies, because when an enemy is killed and a Dungeon Spirit spawns, the piranha will bounce onto it.