Bacteria are ubiquitous in our daily life, existing in soils, sediments, and even our body, frequently as surface-attached biofilm communities. In some cases, biofilms serve a positive purpose, such as improving health or remediating polluted water; in other cases, they negatively impact our lives, such as by causing infection or fouling equipment. For both positive and negative purposes, understanding the factors that regulate the onset of biofilm formation is crucial in determining how to control or treat them. However, how bacteria transition between the free-swimming planktonic state to the sedentary biofilm state in these heterogeneous environments is poorly understood. Here, we use computational modeling to investigate how biofilm formation depends on bacterial properties as well as the properties of their environment. Specifically, by analyzing the competition between chemotactic dispersal and quorum sensing, we establish universal rules predicting how the onset and extent of biofilm formation depend on cell concentration and motility; nutrient, diffusion, and consumption; chemotactic sensing; and autoinducer secretion. The findings from this study therefore yield quantitative principles by which biofilm formation can be predicted and controlled.