"of sound mind, mentally sound," 1721, a back-formation from insane or sanity or else from Latin sanus "sound, healthy," in its figurative or transferred use, "of sound mind, rational, sane," also, of style, "correct;" a word of uncertain origin.
It is perhaps from PIE *seh-no- from *seh- "to tie." That reconstruction "is purely mechanical," according to de Vaan, the meaning might be "which is in place, in order." Or it could be from a different root meaning "to satisfy" as in Latin satis "enough."
Used earlier, of the body, with a sense of "healthy" (1620s), but this has been rare in English. OED writes, "The almost entire restriction in English to a sense 'mentally sound' is due to the use in antithesis with insane, which (like the Latin insanus, its source) always referred to mental condition." Related: Sanely; saneness.