There are many half-baked answers to this question on the internet, so I will try to make this answer thorough. I use a deliberately vague definition at the start and slowly build up from there, so you don’t have to untangle everything as you go.
What are tiers?
Tiers are a ranking system for the effectiveness of each Pokémon in competitive play. Generally, a Pokémon in a higher tier is viewed by competitive players to be better than a Pokémon in a lower tier.
Tiers in Pokémon (as we know them) were created by a community called Smogon in the mid-00s, and are still run on the Smogon Forums. All the official tiers for single battles are listed below, in order of strength (i.e. Ubers is highest and PU is lowest).
Each tier also doubles as a playable battle format. A battle format (or simply ‘format’) is a ruleset that specifies what Pokémon are allowed, what rules are in place, etc. during a battle. When playing a battle in a particular tier, you are only allowed to use Pokémon that are rated within that tier, or rated below that tier.
For example, if I’m playing Gen 8 OU, then I can use Dragapult because it is rated OU in Gen 8. I can also use Conkeldurr because it is rated UU in Gen 8. However, I cannot use Palkia, because Palkia is rated Ubers in Gen 8. To see a list of Pokémon in each tier, use Smogon’s strategy dex.
These tiered battle formats allow less-viable Pokemon to be used in a competitive setting. Your favourite Pokemon may not have a realistic use in OU (for example), but it could have a real niche in RU. Tiers are also governed by Smogon's clauses, which ban strategies deemed uncompetitive like OHKO moves and evasion moves/abilities. Each individual tier can have its own similar bans, which are always outlined in Smogon's strategy dex when present.
Smogon tiers/formats and their rules are not enforced by the cartridge games themselves, because tiers are fan-made and moderated by fans. To play ranked matches for Smogon tiers/formats, use the popular simulator Pokémon Showdown! (or just ‘Showdown’).
There are also tiers for doubles, called Doubles Ubers, DOU and DUU. These are less popular than the singles tiers because the doubles community generally revolves around VGC.
How do tiers work?
Tiers are fundamentally usage based. This means: if a particular Pokémon is used often enough in a certain tier, then it will become a member of that tier officially. Of course, the highest tiers are the hardest to join, because the best Pokémon are in them. Tiers are not decided by the opinion of a group of people – they’re decided by the habits of real players.
To decide if a Pokémon gets “enough usage” to be part of a particular tier, Smogon uses the rule:
A Pokémon truly belongs to its tier if a typical competitive player is more than 50% likely to encounter that Pokémon at least once in a given day of playing (15 battles).
Mathematically, this means that a Pokémon must maintain at least ~4.52% of total weighted usage in players’ teams for a particular tier to remain in that tier (or get promoted to it). Official usage stats are published by Smogon, so you can see which Pokémon are close to being promoted or demoted. These are taken from ladder matches on Showdown.
Every month, on the month, Smogon administrators officially announce the usage-based changes for the tiers (also called the ‘tier shift’, not to be confused with the battle format Tier Shift). Here is an example of what this looks like. If a Pokémon has failed to make the cutoff for its current tier (~4.52% weighted usage) then it will drop to the tier below. If a Pokémon has achieved the ~4.52% cutoff in a tier higher than its current one, then it will be promoted to that tier.
(ASIDE: A Pokémon in PU can suddenly jump up to OU if it gets enough usage in OU, i.e. skipping multiple tiers. Massive changes like this have happened before – new releases and developments in the metagame can make Pokémon with a niche skillset suddenly more valuable. In this sense, tiers cannot be thought of as a perfect ranking system – ultimately, they are at the mercy of living, breathing metagames that might need certain traits more than others. If OU players really need a specially-defensive Grass-type for some reason, then specially-defensive Grass-types could get more usage in OU compared to UU.)
Though tiers are primarily usage-based, manual bans can also occur. Each official tier has a council on Smogon that, if it deems necessary, can quickban newly-released Pokemon that are clearly problematic, or host a suspect test for a particular Pokémon, item, etc. During a suspect test, a new ladder on Showdown is created with the proposed change (e.g. [x] item is banned, [y] Pokémon is unbanned), and players who get a high enough Elo rating on that ladder will be able to vote on whether the proposed change goes through.
This creates a small complexity: what happens to a Pokémon that is banned from UU, but isn’t officially OU by usage? This is where the ‘borderline’ (BL) tiers come in. A Pokémon banned from UU but not used enough in OU to get promoted is given the tier UUBL (and likewise for RUBL, NUBL, etc.). For example: at the time of writing this, Gengar is banned from Gen 8 UU, but its recent usage in OU is only 2.14%, so it is designated UUBL.
BL tiers are not playable tiers. A Pokémon in UUBL is OU for all intents and purposes – BL tiers are just there to distinct between Pokémon which truly belong in their tier, and those which were ousted from a lower tier. Note, there is no BL tier between Ubers and OU; by tradition, Ubers literally is OU’s banlist, so all Pokémon under ~4.52% usage in Ubers but banned from OU are still formally Ubers.
What about AG, LC, etc.?
AG and LC are both playable battle formats, but they are not part of the tier ecosystem described above. Usage in AG and LC has no bearing on the official tiers (and they have different philosophies as far as their rules are concerned), so they are dealt with separately.
It is still possible to “rank” Pokémon as AG or LC if they don’t belong to an official tier (and Smogon’s strategy dex does this), but be wary that this is not a real assignment. Zacian-Crowned (which is banned from Ubers in Gen 8 and often labelled “AG” as a result) is formally untiered, and so are all of the Pokémon in LC (or otherwise) that don’t get enough usage in any official tier.
Note, this means each Pokémon does not necessarily have a tier. If a Pokémon does not get enough usage in any official tier to become part of an official tier, then it will just stay in untiered purgatory. Such untiered Pokémon can be used in any official tier. (They are sometimes given “ZU”, which is an unofficial tier below PU.)
What should I do?
If you are new to competitive Pokémon, you should get comfortable with tiers, and consider building a team for a tier that you like the look of. For example, if you like Gyarados, then you might want to play UU, because Gyarados is rated UU and is a popular choice in that metagame. Read strategies on Smogon for the tier you’ve chosen and maybe look up teambuilding guides to help you make a structure.
Remember to pick your battle format before teambuilding, so you can respond to common threats in the metagame while you’re teambuilding. Many new players build a team, don’t know where to go, and end up queueing AG ("Anything Goes") – don’t do that!