Seawater intrusion is a major concern commonly found in coastal aquifers worldwide. Because of the intense aquifer exploitation and land-based marine aquaculture in the coastal area of Beihai City, Guangxi Zhuang Autonomous Region, China, numerous underground aquifers in this area have been affected by seawater intrusion. However, the microbial communities in freshwater aquifers and their response to seawater intrusion are still unclear. In this study, groundwater from three aquifers was collected from three monitoring sites at different distances from the coastline in the coastal area of Beihai City, and the hydrochemical characteristics of these groundwater samples and the structure of the associated microbial communities were analyzed. The Cl− concentration of the samples indicated that seawater intrusion had occurred in the research area up to 1.5 km away from the coastline, but the monitoring site 2 km away from the coastline had yet to be affected. Statistical analysis showed that the bacterial communities in different groundwater aquifers were significantly correlated with the Cl− concentration, thereby suggesting that the extent of seawater intrusion might be one of the primary factors shaping bacterial composition in groundwater of this area, but the composition and distribution of archaea did not show a significant response to seawater intrusion and presented no apparent correlation with the Cl− concentration. α-, γ-Proteobacteria and Bacteroidota were the dominant bacterial lineages, accounting for about 58–95% of the bacterial communities. Meanwhile, the predominant archaeal taxa were mainly composed of Crenarchaeota, Nanoarchaeota, and Thermoplasmatota, as accounting for 83–100%. Moreover, there was significant spatial heterogeneity of microbial communities in the aquifers affected by varying degrees of seawater intrusion. The microbial communities inhabiting the unconfined aquifer were influenced by the geochemical fluctuation caused by seawater infiltration from land-based marine aquaculture ponds and the diffusion of eutrophic surface water. In contrast, changes in microbial community structure in the confined aquifers were closely related to the environmental gradient caused by different degrees of seawater intrusion. In addition, we also found that the tidal cycle did not significantly affect the structure of microbial communities inhabiting confined aquifers that had been long affected by seawater intrusion.