From Minecraft Wiki
For the mechanic used with piglins, see Bartering.

Trading is an interaction between the player and villagers, which was added in Java Edition 1.3.1 and Pocket Edition 1.0.4. It's done by right-clicking a villager in the PC version of Minecraft. Baby Villagers and Nitwits are not able to trade. 

How To Trade

File:Screen Shot 2012-08-02 at 4.18.46 PM.png
A Villager who will trade 7 Cooked Chicken for one Emerald.

Villagers and Wandering Traders can trade many items, such as Raw Chicken, Cookies, Wheat, Bottles o' Enchanting, Chain Armor, Diamonds, and Bread. They can trade much more too.

Each villager starts with a single option to trade for something. For this explanation, they will be referred to as "trade agreements," or simply "agreements." Each trade agreement can be done a certain number of times before the villager no longer wants to honor it. This is usually five times, but may vary. Completing the first trade agreement and then closing the trade window or interface, will cause the villager to enter a "thinking" mode, and give the villager an increased reputation with a player (reputation further explained below).

If you trade with a villager as much as possible you will begin to unlock new trades depending on how high the villager's level is. A villager can have up to five trade agreements at one time, though it can replace old ones with new ones after the fifth. The only way to unlock more agreements is to trade with the villager, completing at least one agreement each time. Once five trade agreements have been reached, continuing to trade the final agreement will still reset the other agreements, allowing more trading, but also have a chance of replacing the current agreements with yet another new agreement.

An example of the new trading GUI.

Trading is a fast way of earning Emeralds in survival mode as the Emerald Ore block is very rare.

Cartographer villagers were added in The Exploration Update. Players can trade with these cartographers who are willing to trade an Exploration Map (woodland or ocean) for a combination of emeralds and a Compass.

In multiplayer servers with NPC villagers built-in to villages, players may act as highwaymen, or robbers, and kill players they believe may have just traded a villager for emeralds.

Wandering Traders were added in 1.14 in the Village & Pillage Update. They offer random items such as ice, coral, saplings, etcetera, for a price of 1-3 emeralds. 

Villager Reputation

Main article: Reputation

A villager will "like" a player more, the more times they trade with each other. This is useful for two purposes: to stop the town's Iron Golems from attacking a player, and to make villagers offer discounts. Golems usually only become hostile when the villagers are angry because they were recently attacked by a player. Alternatively, a player may escape the village and wait for a while before returning. Hostile iron golems will eventually forgive/forget a player. Trading is usually difficult when done indoors when iron golems are present and the villagers are angry with a player.