The Pirate Invasion starts with a Pirate Map from Ocean enemies in Hardmode, or by itself at dawn. Here is how to get the map, beat the pirates and farm their rare money items.
You start the Pirate Invasion in Terraria by using a Pirate Map, which has a 1% chance to drop from enemies killed in the Ocean biome in Hardmode. Like the other invasions, it needs at least one player with 200 or more maximum health, and the map is used up.
The pirates also show up on their own at dawn in Hardmode once you have broken an altar. Beating them lets the Pirate move in, and the event drops some of the best money items in the game, like the Lucky Coin and the Coin Gun.

Go to either Ocean in Hardmode and kill whatever spawns there. Each enemy has a 1 in 100 chance to drop a Pirate Map, as long as these are true:

A Water Candle and a Battle Potion on the beach make many more enemies spawn, so the map drops much faster. Pirates themselves can drop Pirate Maps too when you fight them near the Ocean.
A random Pirate Invasion can only start at 4:30 AM in a Hardmode world where at least one altar is broken and no other invasion is running. The Goblin Army roll comes first, and if it fails there is a 1 in 30 chance for pirates.
That works out to about 3.28% each dawn until you beat them once, then 1.64%. A chat message tells you whether they come from the west or the east.
You need 180 kills in single player, plus 60 for each extra player with 200 health. Most pirates count as 1 kill, the Pirate Captain counts as 5 and the Flying Dutchman as 10.
Pirates can't shoot through blocks, so a small bunker of blocks at your spawn keeps you safe while you attack through a gap, or with weapons like the Daedalus Stormbow. Watch out for the Pirate's Curse, which ignores your walls.
For the ship itself, see How To Summon The Flying Dutchman.
Every pirate except Parrots and Pirate's Curses can drop these rare items:

The Flying Dutchman always drops a piece of golden furniture and has a 5% chance for The Dutchman painting. Regular pirates also drop Sailor and Buccaneer vanity pieces and the Eye Patch.
The Gold Ring and Lucky Coin make the Coin Ring at a Tinkerer's Workshop, and adding the Discount Card turns it into the Greedy Ring, one of the best money accessories. Fighting the invasion with a Flask of Gold also makes every pirate drop extra coins.
Note: After the first win, the Pirate can move in as a town NPC. None of the rare drops are in his shop, so the event is the only place to get them.