The author Jonathan Swift used Gulliver's madness to make a satirical comment on the less than ideal qualities found in humanity and how humans can distance themselves from (or not recognize themselves) in it.
Gulliver descends into madness in part IV when he interacts with the Houyhnhnm and Yahoos. He considers the (horse) Houhnhnms to be refined and ideal. However, he recognizes his own human nature in the beast-like (human) Yahoos.
His refusal to acknowledge and accept this is seen when he becomes enamored with the Houyhnhnm culture and the fact that they do not lie, they live in houses, are poetic, avoid evil and treat each other with respect.
The Yahoos are so repugnant to Gulliver and his idea of self that he clings to the Houyhnhnms as a picture of perfection.
He successfully imitates the Houyhnhnm as closely as possible and lives among them.
While he mentally sees that humanity (in the form of Yahoos) can be repulsive, his madness distances him from it. Instead he has pride in being "different," acting like the Houyhnhnms he so admires.
Gulliver planned on spending the rest of his days with the Houyhnhnms practicing their virtues. Then, something entirely out of his control happens when the Houyhnhnms ask him to depart and he is banished for being a Yahoo.
He had come to regard his friends and family back home as disgusting as the Yahoos. When he eventually returns home he cannot stand the sight or smell of them, preferring the company of horses.