Ectoplasm is a crafting material dropped from Dungeon Spirits, which spawn rarely from any mob that has 100 HP or more. This can only occur in (Hard Mode) Dungeon after Plantera has been defeated.
A good way to farm for Dungeon Spirits is to build a cage in the center of two hallways. This allows a player to pool enemies underneath them, while still being able to see all around themselves. The cage helps fight Necromancers since they move after being hit once.
It is advised to make sure you have a weapon that can move through walls (example: Death Sickle). Now it's simple. Just kill the enemies as they spawn and this should, safely, give you around 50 Ectoplasm per hour.