Companies also take into account if the thing they're parodying is defacing the original property to the point that will ruin their business. If they are, they will sue. If not, then it's usually okay.
Also, if they're profiting off of someone else's franchise, that will bring up a lawsuit as well.
But luckily, shows like the ones mentioned in the first post have lawyers that look over everything before going into production to ensure that they'll be lawsuit free.