The narrow self interest of a small group isn't always compatible with the larger group, or even the small group's own longer term interest.
If the ride does not terminate in x or y, and is using an ordinary passenger car, I doubt that such a regulation would be enforceable specifically against ride services unless the town could show a specific danger from them. I think they would have to charge $5 for every non-resident vehicle passing through, not just ride service vehicles, to get away with it.
It could be enforced the same way any other traffic-related law gets enforced: police can pull you over, and impound your car if you aren't registered. Sure, not everyone would get caught, but it would be a stiff enough penalty that drivers would either comply or avoid the town. I'm not saying it would be practical, but this is the sort of law you'd get if every town is allowed to make their own rules about such things.
Hi, part time taxi driver here. We still do prearranged pickups at airports where it might not be legal. Even with the potential risk of $1000+ tickets hanging over our heads.
This logic is the same reason the drug war failed. If there's demand for a better/more effective service and we can still profit off it, we will provide it despite legal challenges.