Understand that using this phrase often points to a social mistake that's awkward or taboo. When he mentioned her divorce, he really put his foot in it.
Pro Tip 2/3
Apology Trigger
It's usually followed by an apology or an attempt to correct the awkward situation. After commenting on her haircut, he realized he'd put his foot in it and quickly apologized.
Pro Tip 3/3
Informal Expression
It's an informal idiom, more likely to be used in personal conversations than in formal contexts. I didn't know he had failed his exams; I really put my foot in it.