The etymology of humor shows it represented bodily fluids. It is understood that there was a belief in the ancient time, when each type of bodily fluids were attributed to particular state of mind. But it is not clear how it ended up representing something funny.
According to the humors theory of physiology, both mental and physical health are dependent on a balance of the four primary humors: bile, black bile, phlegm and blood. A person who has an excess of one humor and a deficiency of the others is out of balance and is accordingly spoken of (still) as being out of temper (Latin temperare, 'mix') or as having a melancholy, bilious, phlegmatic or sanguine temperament.
Humor thus came to be a synonym for imbalance or eccentricity of character, the stock-in-trade of intellectual Renaissance comedy. Ben Jonson for instance wrote a popular play, Every Man In His Humour, and a sequel, Every Man Out of His Humour; the prologue of the latter defines the comic treatment:
Some one peculiar quality
Doth so possess a man, that it doth draw
All his affects, his spirits, and his powers,
In their confluctions, all to run one way.
Over the next century, humor passed by degrees from signifying what makes a character ridiculous to anything which excites more or less sympathetic laughter.