Inflammatory bowel disease (IBD), which includes Crohn’s disease and ulcerative colitis, is a chronic, relapsing immune-mediated disease with a varying and sometimes severe disease course. IBD is often diagnosed in early adulthood and can lead to a substantial decline in quality of life. It has been suggested that patients with IBD are at increased risk of depression and anxiety, but it is still unclear to what extent these diseases co-occur and in what sequence they arise. This Review summarizes the literature on the degree of co-occurrence of IBD with depression and anxiety and the temporal relationship between these diseases. We also discuss the effect of psychological stress on the onset and course of IBD. In addition, we outline the possible mechanisms underlying the co-occurrence of IBD and depression and anxiety, which include changes in brain signalling and morphology, increases in peripheral and intracerebral pro-inflammatory cytokines, impairment of the nitric oxide pathway, changes in vagal nerve signalling, gut dysbiosis and genetics. Finally, we examine the possible effects of treatment of depression and anxiety on the risk and course of IBD, the influence of psychological interventions on IBD, and the effects of IBD treatment on psychiatric comorbidity.