Beneficial bacteria have been shown to affect host longevity, but the molecular mechanisms mediating such effects remain largely unclear. Here we show that formation of Bacillus subtilis biofilms increases Caenorhabditis elegans lifespan. Biofilm-proficient B. subtilis colonizes the C. elegans gut and extends worm lifespan more than biofilm-deficient isogenic strains. Two molecules produced by B. subtilis - the quorum-sensing pentapeptide CSF and nitric oxide (NO) - are sufficient to extend C. elegans longevity. When B. subtilis is cultured under biofilm-supporting conditions, the synthesis of NO and CSF is increased in comparison with their production under planktonic growth conditions. We further show that the prolongevity effect of B. subtilis biofilms depends on the DAF-2/DAF-16/HSF-1 signalling axis and the downregulation of the insulin-like signalling (ILS) pathway.