Related Articles
Offensive Coverage
What makes a good PvP team? On offense, it should hit as many targets with super effective damage as possible. This brings us the first team building technique.
There are 97 unique type combinations for all Pokémon up to Gen 4. To cover every one of them with super effective damage (namely, perfect coverage), it requires at least 9 attacking types. Such 9type set is not unique; in fact, there are 2 of them:
{Dark, Fairy, Fighting, Fire, Grass, Ground, Ice, Poison, Rock}
{Dark, Fairy, Fighting, Fire, Grass, Ground, Poison, Rock, Water}
Is it possible to build a team that fulfills perfect coverage? The answer is yes. An example that fulfills the former set is:

Roserade: Razor Leaf + Sludge Bomb + Dazzling Gleam

Piloswine: Ice Shard + Bulldoze + Stone Edge

Mew: Snarl + Overheat + Focus Blast
While this team achieves perfect coverage, it's far from perfect. These Pokémon lack optimal stats for PvP and the quality of the moves is suspect. From a defensive perspective, our team is highly weak to Fire since no member resists while two are weak to it. Some Fast Attacks don't do much damage and mostly serve to gain energy, so they can't really be considered to provide helpful coverage.
The takeaway is that it's often not recommended to go for perfect coverage. Instead, we may focus on a subset of types that are metarelevant. For instance, the Master League meta centers around these Pokémon; in particular, Ground, Steel, Flying, Dragon, Psychic, Dark, and Ghost types. You may want to double or triple cover for these types while ignoring types like Bug, who are weak in Master League.
There are online tools (such as this one) that can help you visualize the coverage and weaknesses of your team. Keep in mind that a good team is always a tradeoff; the wider the coverage, the less specialized your team becomes.
Defensive Synergy
An important defensive team building technique is team hedging. The goal is to have team members cover each others' back. The definition of "cover" is:
A covers B if A is the counter to the counter to B.
For the purpose of illustration, we follow the conventional graphical notations:
Let’s first discuss how to find a Pokémon’s best counter in PvP. It’s pretty much the same as before, except that we don’t have prior knowledge on the moves of your opponent, and that in a match the opponent will always choose the one the more effective Charged Attack. As such, the best counter should maximize the minimum performance against different movesets.
For example, against Melmetal, some candidates’ TDO are (data from Comprehensive DPS/TDO spreadsheet):
Counter  Rock Slide  T.bolt  Flash Cannon  Hyper Beam  Minimum 

Groudon  573.8  865.7  306.2  336.2  306.2 
Rhyperior  415.9  639.6  108.4  397  108.4 
Swampert  359.1  359.1  316.9  208.2  208.2 
Heatran  224.1  224.1  486.1  349  224.1 
While Heatran tanks Flash Cannon, it fares less well against Rock Slide or Thunderbolt. Similarly, while Rhyperior is the best against Hyper Beam, it is very weak to Flash Cannon. The winner here is Groudon. If you have prior knowledge on what Melmetal's Charged Attacks are, the conclusion may change. For example, if you know that the opponent Melmetal has Rock Slide and Flash Cannon, then the best counter would be Swampert.
With the method to find out a Pokémon's best counter ready, let's start with the first type of team hedging: circlehedged.
A team {A, B, C} is circlehedged if:

A covers B;

B covers C;

C covers A.
How to find such {A, B, C}? We start from any Pokémon. Then we repeatedly find the best counter to the previous Pokémon and grow the counter chain:
It will eventually form a loop since the best counter is unique and there are finite Pokémon. To build a circlehedged team, we need to identify a loop of size 1, 3 or 6:

If the loop is of size 1, then include 3 copies of that Pokemon.

If the loop is of size 3, then include all of them.

If the loop is of size 6, then include the nonadjacent Pokemon.
Here's a concrete example. If we start with the highest TDO Pokémon, Lugia, the minimum table method will tell us that:

Lugia's best counter is Melmetal

Melmetal's best counter is Groudon

Groudon’s best counter is Lugia
Therefore we now have a size 3 counter loop: Lugia, Melmetal, Groudon. These three make a circlehedged team: if any team member finds itself in its worst matchup, you can switch to turn the tables. However, be aware that Pokémon have multiple counters, and circlehedging can expose blind spots. For example, this team has 2 members weak to Ice. Avalanche + Bulldoze Mamoswine can hit all members of this team for super effective damage.
On a side note, the notion can be generalized as a classic "RockPaperScissor" categorization:
 Scissor: Flyingtypes. Most of them have wings and look like scissors. Example: Lugia, Dragonite, Gyarados
 Rock: Rocktypes and Electrictypes. They beat Flyingtypes. Example: Melmetal, Raikou, Tyranitar, Regirock
 Paper: Groundtypes. Ground is flat and so is paper. They beat Rocks and are beaten by Flying. Example: Groudon, Swampert
This is one such example of a type triangle. There's also the classic Fire, Water, Grass triangle of the starter Pokémon and the Fighting, Psychic, Dark / Ghost triangle.
What if we identify a counter loop of size 4 in the counter chain? The brings us another type of teamhedging suggested by dondon, doublyhedged:
 A covers B;
 B covers A;
 C covers both A and B.
The trick is to find a proper Pokémon C that cover both A and B. An example is Cresselia, Umbreon, Azumarill. Cresselia and Umbreon cover each other, while Azumarill counters Dark (covering Cresselia) and Fighting (covering Umbreon).
Complementarity
A Pokémon usually has multiple counters and is seldom fully covered by another Pokémon. In our example, while Groudon covers Lugia by countering Melmetal  the best counter to Lugia, it doesn’t counter Icetypes, another threat to Lugia. We would like to systematically measure the synergy between any two Pokémon, factoring:

How well they cover each other

How overlapping their strengths and weaknesses are
We propose a metric for it  Complementarity.
Using the same graphical notation:
Within the scope of this article, A counters B means that A defeats B in battle, regardless by how much.
The weight on the edge, w, is a positive number measuring the extent to which A defeats B. We use the battle score of A when fighting against B (more on battle score here) for the weight. By calculating all battle scores between every two Pokémon from the pool, we obtain a battle score matrix W.
Suppose we are given Pokémon A and B. For any Pokémon C that is not A or B, there are four possible cases:
For case 1, the higher the weight of A countering C, $W_{A,C}$, the better A covers B. In the meantime, the higher $W_{C,B}$, the more significant $W_{A,C}$ is: naturally, how A performs against B’s worse counters matters more. Intuition suggests using the product of $W_{A,C}$ and $W_{C,B}$ to measure how well A covers B in terms of the intermediate C. This is similar for case 2. We then add these weight products up.
For case 3, since A and B share a common weakness, we give penalty by subtracting the $W_{C,A} \cdot W_{C,B}$ from the product sum. This is equivalent to adding $W_{A,C} \cdot W_{C,B}$ since $W_{A,C} = W_{C,A}$. For case 4, we give the same penalty. A overlapping strength puts more risk to the team since its performance depends more on the opponent.
Finally, the Complementarity between A and B is defined as:
$$ \sum_{C} {W_{A,C} \cdot W_{C,B}}$$Note: The above formula does not specify that $C$ isn't $A$ or $B$. However, since $W_{A,A} = W_{B,B} = 0$, allowing $C$ to be $A$ or $B$ does not affect the complementarity. For the sake of simplicity (and another nice property to be discussed below), we choose to do so.
Does the formula look awfully familiar? Yes, it is exactly matrix multiplication! Therefore, if we square the battle score matrix $W$, the result will be the Complementarity matrix.
Here’s a concrete example of 13 meta Pokémon in Master Tier. The battle score matrix and complementarity matrix have been computed in the spreadsheet linked above. Simulation data are from GoBattleSim and 1 shield for each side is used.
Which Pokémon pair have the highest complementarity? The answer is Groudon and Lugia. Highlighting these two in the following graph:
The GroudonMelmetalLugia path contributes the most to the complementarity. Other significant paths include GroudonMetagrossLugia, GroudonTyranitarLugia, LugiaDragoniteGroudon, LugiaSwampertGroudon. While Groudon and Lugia do share common weakness (most noticeable of which is Ice Beam Mewtwo) and strengths, their complementarity remains the highest.
Here are some other high complementarity Pokémon pairs:

Melmetal, Swampert

Melmetal, Mewtwo

Metagross, Kyogre
And here are some low complementarity Pokémon pairs:

Melmetal, Metagross

Groudon, Heatran

Lugia, Mewtwo
From the riskaverse perspective, it is preferred to include high complementarity Pokémon pairs in your team. Keep in mind that lowcomplementarity Pokémon pairs aren’t necessarily “bad”. While they share more common weaknesses and strengths, they are more specialized against a specific group of targets.
Greedy Strategies
The teamhedging methods provide balanced teams that have high complementarity and low risk. However, higher the risk, higher the potential return when your guess is correct. We’ll introduce some riskseeking team building techniques.
Suppose you know that your opponent won’t use any Groundtype. How would you choose your team? One answer provided by Ryan is Melmetal, Heatran, Giratina. Nothing really beats them other than Groundtypes. They cover each other’s back, too, and outperform the Melmetal, Groudon, Lugia team in the "noGroundtype" realm. Of course, if you are wrong and your opponent brings Groudon, then your team is pretty much doomed.
This technique, “hedging on your opponent”, is very useful when you have prior knowledge on what cards your friends have and wish to leverage it. The systematic way of doing so is similar to the minimum performance table method above, except that we may ignore some matchups according to our guess and thereby update the conclusion.
If you’d like to take more risk, you may consider doubling up or even tripling up. For example, if you bet your opponent won’t use Flyingtypes, then nothing is stopping you from bringing in two or three Groudon. Likewise, if you bet your opponent won’t bring Tyranitar or any other Darktype, you are free to roll with two or three Giratina.
The philosophy behind this is that some Pokémon are just that strong  such as Groudon, Giratina, and Lugia  when their counters are not present, they are unstoppable. In a game of RPS, three Rocks can overcome {Rock, Paper, Scissors} if Rock beats Scissors more than Paper beats Rock.
The secret here is to take risk wisely, not blindly. Don’t go in with three Machamp because a) hello there, it’s PvP, not gyms; b) Even if you anticipate three Tyranitar, just one Machamp will be enough to subdue them. Similar for Tyranitar: two are often enough to chew through three of anything weak to Dark. Take only those risks that bring extra return.
Conclusion
There are 3,840 different individual Pokémon considering just single Charged Attack, which makes at least 28,311,552,000 different team compositions in total. What is the absolute best team among these 28 billion teams? We don’t know. To precisely answer that, we need to run around 4*10^20 simulations for every pair for every team. But even then, after removing the dominated teams, we have a giant matrix to solve for the equilibrium weights. Finding the global optimum is a challenging task, but I won’t stop trying.
The techniques introduced in this articles may not yield the globally optimal team. Their results are a local optimum from a particular perspective, whether it be wide coverage, teamhedging, or greedy strategies. Nevertheless, these algorithms provide reasonably good solutions and are more computationally efficient. More importantly, 授人以鱼不如授人以渔; the team examples in this article might become obsolete after the next stat/move rebalance, but if you have mastered the techniques, you can always find the right teams yourself easily.
After you and your opponent set the teams and enter the battle, what is your optimal play? When should you use a shield? When should you switch? What are some other tricks that give you an edge in competitive play? All these will be covered in our next and final topic, execution. Stay tuned!