You can easily end up with leftovers from a prescription if stop taking them midway through the course once you're feeling better. Those left overs can then be used to self-medicate.
I could speculate on how else it happens, but the important part is that it does happen, and e.g. the Mayo clinic says:
> According to the Centers for Disease Control and Prevention, about one-third of antibiotic use in people is not needed nor appropriate.
https://www.mayoclinic.org/healthy-lifestyle/consumer-health...