To me garlic acts as an anti-bacterial, so if it prevents getting worms then it would work as worms to me come from bacteria. If the gut is healthy then worms won't be there. Sounds simple but I don't think it's that complicated. Apple cider vinegar is also a preventative it helps to neutralise the gut. If you have worms you have to chemically remove them, so best not to get them in the first place.
Worms are not bacteria, they are helminths. Unless you keep birds, in a sterile environment from hatch until death then worms are perfectly normal - its worm overload that causes issues.
Garlics antimicrobial attributes have the ability to kill bacteria,viruses,fungi,mold and parasites according to Susan Burke a Herbalist. She also says it is used as a preventative care and treatments of various ailments such as internal parasites, respiratory illness, wounds, bumble foot and crop problems.
Check out the link below. I'm willing to try anything apart from chemicals if I can help it. Sometimes we need the chemicals, but I'd prefer not to if I have an alternative. It's interesting reading.