Sometimes a special ability or spell tells you that you have Advantage or Disadvantage on a d20 Test. When that happens, you roll a second d20 when you make the roll. Use the higher of the two rolls if you have Advantage, and use the lower roll if you have Disadvantage. For example, if you have Disadvantage and roll a 17 and a 5, you use the 5. If you instead have Advantage and roll those numbers, you use the 17.
If multiple situations affect a roll and each one grants Advantage or imposes Disadvantage on it, you don't roll more than one additional d20. If two favorable situations grant Advantage, for example, you still roll only one additional d20.
If circumstances cause a roll to have both Advantage and Disadvantage, you are considered to have neither of them, and you roll one d20. This is true even if multiple circumstances impose Disadvantage and only one grants Advantage or vice versa. In such a situation, you have neither Advantage nor Disadvantage.
When you have Advantage or Disadvantage and something in the game, such as the halfling's Lucky trait, lets you reroll or replace the d20, you can reroll or replace only one of the dice. You choose which one. For example, if a halfling has Advantage or Disadvantage on an ability check and rolls a 1 and a 13, the halfling could use the Lucky trait to reroll the 1.
You usually gain Advantage or Disadvantage through the use of special abilities, actions, or spells. Heroic Inspiration can also give a character Advantage. The DM can also decide that circumstances influence a roll in one direction or the other and grant Advantage or impose Disadvantage as a result.
In terms of its affect on the game and the outcome of d20 Tests, Advantage and Disadvantage are among the most useful tools in adjudicating situations in play. It can be used to reflect temporary circumstances that might affect the chances of a character succeeding or failing at a task. Advantage is also a great way to reward a player who shows exceptional creativity in play.
Characters often gain Advantage or Disadvantage through the use of special abilities, actions, spells, or other features of their classes or backgrounds. In other cases, you can decide whether a circumstance influences a roll in one direction or another, and can grant Advantage or impose Disadvantage as a result.
Consider granting advantage when...
Consider imposing disadvantage when...
Because Advantage and Disadvantage cancel each other out, there's no need to keep track of how many circumstances weigh on both sides.
For example, imagine a wizard is running down a dungeon corridor to escape from a beholder. Around the corner ahead, two ogres lie in wait. Does the wizard hear the ogres readying their ambush? You look at the wizard's passive Wisdom (Perception) score and consider all the factors weighing on it.
The wizard is running, not paying attention to what's ahead of him. This imposes Disadvantage on the wizard's ability check. However, the ogres are readying a portcullis trap and making a lot of noise with a winch, which could grant the wizard Advantage on the check. As a result, the character has neither Advantage nor Disadvantage on the Wisdom check, and you don't need to consider any additional factors. Past encounters with an ogre ambush, the fact that the wizard's ears are still ringing from the Thunderwave spell he cast at the Beholder, the overall noise level of the dungeon-none of that matters any more. They all cancel out.
If players and DMs desire more granularity in a situation like the above, consider the Variant Rule for Boons & Banes.
There are lots of ways to get Advantage in the game. Having the right tool for the job (like a crowbar to force open a door with strength) is one way, or having a skill and a complimentary tool proficiency (like Animal Handling and Land Vehicles when driving a cart). To that end, because there are so many permutations, if a player thinks they should get Advantage on a roll, they can ask. The table to can talk it out together and make the final determination.
The same can also be said for Disadvantage. In the above example of the Wizard running through a dungeon, there are a multitude of ways in which Disadvantage might effectively be applied to a roll. Thus, if a player or the DM thinks a particular action should be undertaken with Disadvantage, they can similarly propose the circumstance and talk it out.
In either case, these discussions should be quick and to the point, with the express purpose of making a determination and then moving on, so as not to bog down the speed of play.