Cognitive impairment in various mental illnesses, particularly neuropsychiatric disorders, has adverse functional and clinical consequences. While genetic mutations and epigenetic dysregulations of several genes during embryonic and adult periods are linked to cognitive impairment in mental disorders, the composition and diversity of resident bacteria in the gastrointestinal tract—shaped by environmental factors—also influence the brain epigenome, affecting behavior and cognitive functions. Accordingly, many recent studies have provided evidence that human gut microbiota may offer a potential avenue for improving cognitive deficits. In this review, we provide an overview of the relationship between cognitive impairment, alterations in the gut microbiome, and epigenetic alterations during embryonic and adult periods. We examine how various factors beyond genetics—such as lifestyle, age, and maternal diet—impact the composition, diversity, and epigenetic functionality of the gut microbiome, consequently influencing cognitive performance. Additionally, we explore the potential of maternal gut microbiome signatures and epigenetic biomarkers for predicting cognitive impairment risk in older adults. This article also explores the potential roles of nutritional deficiencies in programming cognitive disorders during the perinatal period in offspring, as well as the promise of gut microbiome-targeted therapeutics with epigenetic effects to prevent or alleviate cognitive dysfunctions in infants, middle-aged adults, and older adults. Unsolved challenges of gut microbiome-targeted therapeutics in mitigating cognitive dysfunctions for translation into clinical practice are discussed, lastly.