A healthy gut microbiota is a dense, interconnected microbial community
"Good" commensal bacteria secrete antimicrobial peptides that kill disease-causing pathogens and stimulate the host's immune response
When a pathogen comes in, there's a bristling army of defenses waiting to kill it...
And even if it did survive, it has nowhere to grow, so it'll either get washed out of the intestine or have to invade the rest of your body.
This protective state of the gut is called colonization resistance.
When we kill off our microbiota with antibiotics, pathogens have plenty of room to grow and they quickly take over.