The term you're looking for is quodlibet, which is a composition that combines several melodies.
There are different types of quodlibets, but you're discussing the "simultaneous quodlibet," when different melodies are played at the same time so as to sound harmonious.
This practice dates back to the Renaissance era, but the tradition is alive and well today, especially in Broadway musicals.
At some level there are important properties the melodies must have for this to work. But since melodies can be combined in so many ways—which melody is above which, are they played at the same tempo (or is one, say, in half time), are there slight changes made at cadence points, etc.—that it's frankly tough to pinpoint what these properties would be. Instead, it's up to the composer to show their mastery and make the melodies fit.
I'm a classical guy, and my favorite quodlibet is at the climax of Benjamin Britten's "Young Person's Guide to the Orchestra." Not only is this a quodlibet, but it's at the ending of what is in my opinion the most impressive fugue ever written. He combines it with a theme of Henry Purcell that started the very beginning of the piece.