In most habitats, microbial life is organized in biofilms, three-dimensional edifices sustained by extracellular polymeric substances that enable bacteria to resist harsh and changing environments. Under multispecies conditions, bacteria can benefit from the polymers produced by other species ("public goods"), thus improving their survival under toxic conditions. A recent study showed that a Bacillus subtilis hospital isolate (NDmed) was able to protect Staphylococcus aureus from biocide action in multispecies biofilms. In this work, we identified ypqP, a gene whose product is required in NDmed for thick-biofilm formation on submerged surfaces and for resistance to two biocides widely used in hospitals. NDmed and S. aureus formed mixed biofilms, and both their spatial arrangement and pathogen protection were mediated by YpqP. Functional ypqP is present in other natural B. subtilis biofilm-forming isolates. However, the gene is disrupted by the SPβ prophage in the weak submerged-biofilm-forming strains NCIB3610 and 168, which are both less resistant than NDmed to the biocides tested. Furthermore, in a 168 laboratory strain cured of the SPβ prophage, the reestablishment of a functional ypqP gene led to increased thickness and resistance to biocides of the associated biofilms. We therefore propose that YpqP is a new and important determinant of B. subtilis surface biofilm architecture, protection against exposure to toxic compounds, and social behavior in bacterial communities.