Find some cadavers (often dropped by NPC bandits from those "deal with bandits" tenant quests or in Avian space pyramids), and perform autopsies on them (Kevin's lab has an Autopsy Table).
Write (and then read) weird/perverted books at Tome Dais station.
Stand too close to maddening pictures/statues that you sometimes find in Elder-themed biomes, or carry such items in your inventory. Some of these pictures you can draw yourself.
Spend it: research some nodes of Metaphysics tree. Also craft weird things like Psionic Battery. You need to be quite mad to do these things, and required Madness is consumed in the process.
Visit Insta-Freud (there is one at the Science Outpost), you rapidly lose accumulated Madness when inside.
Madness slowly decays over time.
Carry around a lot of unspent Protheon. It increases the rate of madness loss.
Consequences
Having more than 50 Madness will sometimes trigger random debuffs (and, more rarely, strong short-term buffs). It's not possible to stop these effects (other than reducing Madness to 0), as nothing grants full immunity to Madness. However, the severity of these debuffs greatly varies. At low Madness you'll only experience mild/harmless effects.
Mental Resistance
Things that grant "Mental Resistance" (such as Counsellor/Psychologist/Psychiatrist crew members or foods like Snarshed Garflipp) are decreasing maximum severity of possible debuffs (meaning that the worst possible debuffs can't happen).
Mental Resistance also causes madness-giving paintings (Cthulhu portraits, etc.) to give Madness less frequently (so it's less of a problem to carry them in your inventory).
Difference between Madness and Insanity
These are two different things. Insanity is a debuff, Madness is a mechanic. They are often confused for each other because of the following:
When you are on Atropus worlds, etc., you experience a permanent Insanity debuff (also known as "Maddening Atmosphere"). This is bad on its own (defense/energy gets reduced over time, flying letters on the screen, etc.), but while you are insane, you also get small amounts of Madness (currency) over time. Once you have accumulated that currency, Madness events can happen. But they are not caused by Insanity directly.
After you accumulate enough Madness currency to start getting random debuffs, one of the possible debuffs is Insanity (decreasing defense/energy over time, flying letters on the screen). So Madness is not the same as Insanity: Madness sometimes causes Insanity (alongside 50+ other things).
Research (unlocking recipes) · Madness (amount of crazy ideas) · Genes (knowledge "how living beings work") · Protheon (insight into great knowledge of the past)