Toxoplasma gondii is a zoonotic protozoan of worldwide distribution. The present study provides information on risk factors affecting T. gondii infection in domestic and free-ranging wild ungulates sharing habitats in Mediterranean ecosystems in Spain. Serum samples from 482 extensively reared domestic ruminants and 2351 wild ungulates were tested for T. gondii antibodies using the modified agglutination test (MAT, cut-off 1:25). Toxoplasma gondii seroprevalence was 41.2% of 194 sheep, 18.6% of 199 cattle and 5.6% of 89 goats. The main risk factors associated with infection in livestock were the presence of cats, feeding on the ground and at stubble fields. In wild ungulates, T. gondii antibodies were detected in 10.5% of 1063 red deer, 15.6% of 294 fallow deer, 5.6% of 216 European mouflon, 5.6% of 90 Spanish ibex, 13.6% of 22 roe deer and 18.6% of 666 wild boars. The risk factors affecting T. gondii infection in wildlife were species, age and hunting season. Significantly higher seroprevalence was found in domestic ruminants, particularly in sheep, compared to the wild species tested. The present study indicates widespread exposure to T. gondii among domestic and wild ungulates in Southern Spain, with significant differences among species sharing the same ecosystem. The high seroprevalence observed in domestic ruminants, particularly in sheep, reinforces the need for farm management practices to control the risk factors associated with T. gondii infection in extensively reared livestock. Consumption of raw and undercooked food products from domestic and wildlife species may have important implications for public health.