This is going to be kind of a rambly answer, partially copypasta from something I've written elsewhere:
Someone else said: An example is a sandwich that is made with roast beef, boursin cheese and caramelized onions.
Well.. that’s a fairly classic combination.
Here’s why, roughly.
Cheese contains a lot of the fifth flavour sense, umami. This sense is, roughly, ‘savoury’; that is, those things you eat that have a great deal of satisfaction, essentially. Tomatoes, cheeses, anything fermented–these are high in umami. One of the things that umami does is to heighten and enhance ‘meaty’ flavours in your food. So pairing cheese with beef becomes more than additive, it is multiplicative; the cheese enhances the flavour of the beef.
Likewise, caramelized onions are full of complex flavours due to transformation of the sugars within the onion. Consider the vast flavour difference between white sugar and caramel. The complex flavours arise from heating the sugars. This is, by the way, the real reason why you sear meat in a pan before roasting it in the oven. Browning the proteins in the meat is known as the Maillard reaction, and creates more complex, intensely savory flavours. Adding caramelized onions (or, classically with a roast, roasted potatoes and onions) plays off those flavours.
Moving on to ‘how the hell do they do that?’
Tasty food (ignoring texture) is built on two things: complement and contrast, similar to basic understanding of art.
Let’s start with the example given of duck and blueberries. These are flavours that contrast; the fatty meaty richness of the duck with the tart-sweet astringency of blueberries. Duck with fruit is a classic pairing from the mists of time; the acid of the fruit cuts through the unctuous mouthfeel of the fat while the sweetness offers a counterpoint to the savoury flavours found in duck meat, enhancing the flavour by contrast. There are, of course, infinite combinations of this. Consider very everyday examples: beer nuts (salty peanuts with a sweet coating), ice cream sundaes (cold sweet solid ice cream with hot slightly bitter liquid chocolate/fudge sauce), or the MetaFilter favourite of peanut butter and pickle sandwiches (soft creamy salty-sweet peanut butter with crunchy sour pickles). In each case, the contrasts enhance each other; in the duck example the sweetness of the blueberries makes the duck seem more savoury while the duck makes the blueberries seem sweeter.
Then there are flavours that complement each other. The easiest to understand is the combination of coffee and chocolate. Each brings dark, roasted, complex flavours to the table which marry incredibly well with each other because they match. And then there is (unless using wholly unsweetened chocolate) the contrast between bitter coffee and sweet chocolate, each flavour playing off the other.
So when you are looking at flavour combinations, you want to look at three things:
1) Flavours which contrast each other: sour/sweet, salty/sweet, fatty/acidic. The list goes on.
2) Flavours which complement each other (more below).
3) And the gestalt; flavours which both contrast and complement, as with the coffee/chocolate example.
Finding contrasting flavours is relatively simple. But note that you are not looking for diametric opposites, necessarily; the bitterness of asparagus is unlikely to pair nicely with the sweetness of caramel, for example. Which is why you really aim for the gestalt.
Finding complementary flavours, I think, is more difficult. In my view, what you are looking for is a flavour note that is common amongst two or more ingredients, while ensuring that none of the ingredients has wildly clashing notes.
Consider these three ingredients:
The first two go together by way of contrast. Ditto the last two. And turkey with (unsweetened) chocolate would work very well–think about a mole sauce. But all three would (probably) not work well together without a lot of very careful finessing. There’s a theory about any three ingredients, but I can't remember the link.
To bring it to more quantifiable terms, McGee explores a lot about flavour pairing in On Food and Cooking, paying specific attention to volatile compounds which work well together. In many cases, it seems, foods which share compounds go well together; this is how Blumenthal put cauliflower and cocoa together, as they share a dominant compound (I cannot remember what it is). So there's that, when we're talking about aromas. My copy of McGee is currently with a friend, otherwise I'd cite you chapter and verse.
In terms of the five basic (primary) flavours you were talking about, I think it comes down to balance. Sweet is a flavour we are hardwired to seek out, as it promises high caloric input, a useful feature thousands of years ago. And the flavour of 'sweet' seems to balance out any other primary flavour; too sour? balance with sugar. Too salty? add sugar. Too bitter? add sugar. Umami itself seems to provide balance in many cases, and pairs especially well with salt (unsurprising given that MSG is a salt itself).
I'd really like to map flavours based on the primary/secondary/tertiary classification you've mentioned, and then look at popular pairings based on those characteristics.