Through eating infested rabbits in the case of mine but I believe they can also pick up the eggs that are transferred by fleas etc. They are easy to cure, just get a good worming treatment from your vet but make sure you treat the fleas as well or the worms car return!
my dog caught them from eating wild animals. Shes such a killer! For some reason, she hated the taste of dog food and prefers raw meat. So she usually likes to kill birds mice and other animals for her meal. I've triend to stop her many times but shes too carnivorous.