They fly towards the player and will teleport to a random location after they hit the player. They can pass through walls and "black space" to reach the player.
Stand still and swing at it with a sword right before it hits you. Ghosts are knocked back several tiles after being hit, so walking towards them may speed up the process of killing them. Otherwise, wait until they slowly float back towards you for another strike.