Bacteria organize in a variety of collective states, from swarming-rapid surface exploration, to biofilms-highly dense immobile communities attributed to stress resistance. It has been suggested that biofilm and swarming are oppositely controlled, making this transition particularly interesting for understanding the ability of bacterial colonies to adapt to challenging environments. Here, the swarm to biofilm transition is studied in Bacillus subtilis by analyzing the bacterial dynamics both on the individual and collective scales. We show that both biological and physical processes facilitate the transition. A few individual cells that initiate the biofilm program cause nucleation of large, approximately scale-free, stationary aggregates of trapped swarm cells. Around aggregates, cells continue swarming almost unobstructed, while inside, trapped cells are added to the biofilm. While our experimental findings rule out previously suggested purely physical effects as a trigger for biofilm formation, they show how physical processes, such as clustering and jamming, accelerate biofilm formation.