Blackbeard was formerly the second division commander under Portgas D. Ace in the Whitebeard Pirates. Shanks had warned Whitebeard that Blackbeard was using the crew as cover—staying hidden in Whitebeard's massive shadow until he grew strong enough to make his move. That moment came when Blackbeard killed one of their own, forcing him to flee and sparking Ace's personal vendetta against him. Shanks was also the one Blackbeard scarred years earlier, which is partly why Shanks urged Whitebeard to call Ace back; he feared the timing of their confrontation could spell disaster.
After Luffy beat Crocodile in Alabasta, Lafitte saw an opportunity and pushed for Blackbeard to replace him as one of the Shichibukai. The catch was Blackbeard needed to build a fearsome enough reputation first.
That's where Luffy comes in. Blackbeard happened to be in Jaya at the same time, and the two nearly went at it in a tavern over the island's food—a weirdly comedic first meeting given how much they had in common. Despite this, Blackbeard gave Luffy a surprisingly genuine speech about how pirates never stop dreaming. The real story came out later: Blackbeard had been hunting for a strong pirate to establish himself, and once he learned Luffy had a 100 million Belli bounty, he decided Luffy was the perfect target. He never got the chance though—the Straw Hats got caught by the knock-up stream and shot straight to Skypiea.
Content compiled by AnimeList.moe from publicly available sources.