Bacteroides fragilis is a prominent member of the human gut microbiota, playing crucial roles in maintaining gut homeostasis and host health. Although it primarily functions as a beneficial commensal, B. fragilis can become pathogenic. To determine the genetic basis of its duality, we conducted a comparative genomic analysis of 813 B. fragilis strains, representing both commensal and pathogenic origins. Our findings reveal that pathogenic strains emerge across diverse phylogenetic lineages, due in part to rapid gene exchange and the adaptability of the accessory genome. We identified 16 phylogenetic groups, differentiated by genes associated with capsule composition, interspecies competition, and host interactions. A microbial genome-wide association study identified 44 genes linked to extra-intestinal survival and pathogenicity. These findings reveal how genomic diversity within commensal species can lead to the emergence of pathogenic traits, broadening our understanding of microbial evolution in the gut.