You are correct. Your immune system kills pathogens but sometimes infection takes hold and the immune response can’t diffuse easily into the affected area. It’s a ferociously complex process but I recommend watching kurtgezat on YouTube (if that’s spelled correctly) as they have a good eli5 type video.
Latest Answers