Kazimierz Dabrowski had a great theory (the Theory of Positive Disintegration) about the relationship between psychoneuroses, the most common symptoms of mental illness, and the developmental trajectories of gifted people. In brief, gifted people are more likely to live out-of-sync with their social environments (too smart, too intense, too righteous, too sensitive). This causes conflict with individuals and the world at large. Because they live in a state of inner and outer conflict due to their essential differences, they develop psychoneuroses (anxiety, obsessions, existential depression etc). These neuroses are evidence for positive disintegration in the gifted, as the process of becoming one's own person in a world that isn't made for the 'Scary smart' is psychologically painful. Many never pass the disintegrative phase, and live with these symptoms perpetually.
So I imagine that the correlation between smarts/creativity and mental illness arises from their characteristic unwillingness to abandon the parts of themselves that set them up to be at odds with 'normal' life. Those are the same intensities that make them exceptional. The upside is that unwanted symptoms of positive disintegration can be worked through and a 'personality ideal' - roughly analogous to a self-actualizing personality a la Maslow - can be achieved.
I hope that gives some insight into the context and dynamics behind that common observation in folk psychology. It's not "smart people are all crazy" but more like "smart people face challenges that most others don't" and that can make them seem crazy while on their hard path of personality development.