The microbial community assembly processes and underlying mechanisms in response to heavy metal accumulation in coastal sediments remain underexplored. In this study, the heavy metal concentration in samples were found below the marine sediment quality standards. Through partial Mantel tests and linear regression analysis, Cd was identified as the major influencing factor, displaying strongest correlation with the bacterial community in the sediments. The class Desulfuromonadia was identified as a biomarker which showed enrichment in the sediments with high Cd content. Additionally, the results of null model and the neutral community model demonstrated the prominent role of stochastic processes in the assembly of bacterial community. However, with the increase in Cd concentration, the influence of selection processes intensified, resulting in a decline in species migration rate and subsequent reduction in ecological niche width. Furthermore, the intensified competition and an increase in keystone species among bacterial populations further enhanced the stability of the microbial co-occurrence network in response to high Cd concentration. This study offers an insight into the effects of heavy metal on microbial assembly and coexistence, which are conducive to marine ecosystem management and conservation.