Our intestines have a beneficial bacterial flora, that is, the bacteria present there do not affect our organism, but they do audit us in some functions. However, when a person has bacterial infection in the intestine, it means that that person has had their body invaded by harmful bacteria that are causing their illness. At this point, all of these bacteria begin to compete for space and food and many of them end up dying in the process.
However, when the patient takes antibiotics to end the infection, these drugs end up killing most of these bacteria, even the beneficial ones, leaving the intestine unbalanced and susceptible to bacteria resistant to this antibiotic.
Resistance happens, because some of these bacteria may not have been exposed correctly or environment, or have been exposed to a non-lethal dose, or have suffered some kind of mutation that makes them resist the drug. In this, the current natural selection, allowing non-resistant bacteria to die, while resistant ones survive and generate other resistant bacteria, creating a flora that is not attacked by the antibiotic.
If the harmful bacteria survive they will create a whole community of harmful bacteria that will cause an even more serious infection.