In the mucosa, the immune system's T cells and B cells have position-specific phenotypes and functions that are influenced by the microbiota. These cells play pivotal parts in the maintenance of immune homeostasis by suppressing responses to harmless antigens and by enforcing the integrity of the barrier functions of the gut mucosa. Imbalances in the gut microbiota, known as dysbiosis, can trigger several immune disorders through the activity of T cells that are both near to and distant from the site of their induction. Elucidation of the mechanisms that distinguish between homeostatic and pathogenic microbiota-host interactions could identify therapeutic targets for preventing or modulating inflammatory diseases and for boosting the efficacy of cancer immunotherapy.