Han won the Millenium Falcon from Lando in a game of Sabacc.
In addition to several thousands of credits, Calrissian bet any one
ship on his lot on Nar Shaddaa. Calrissian almost won the tournament,
only being one card away from holding an Idiot's Array. His final hand
included The Idiot, The Two of Staves and The Seven of Flasks. Solo
won the tournament with The Queen of Air and Darkness, The Five of
Coins, The Six of Staves, and The Master of Coins - Pure Sabacc.
Solo won the 20,000 credits in the sabacc pot, plus roughly the same
amount again he had won throughout the four days. When Calrissian
asked Solo to choose any ship on his lot, Han picked the Millennium
Falcon without hesitation. Calrissian protested, stating that it was
his personal ship and not part of the deal, but Solo insisted that it
was technically "on his lot" and therefore a valid choice. Calrissian
finally conceded, and the Falcon was now the property of Han Solo.
This event was covered in the 3rd book of the Han Solo trilogy Rebel Dawn.
It is aluded to in Empire Strikes Back:
Lando: What are you doing here?
Han Solo: Ah, repairs. I was hoping you could help me out.
Lando: What have you done to my ship?
Han Solo: YOUR ship? Hey, remember you lost her to me, fair and square.
In Dark Apprentice they have a rematch that Lando wins, but to impress Mara Jade he gives the Falcon back to Han. Wookiepdia says:
Soon after, Lando disputed the validity of the sabacc game in which
Han had originally won the Falcon from him, leading to a series of
rematches between them. In the first, Lando won the ship back from Han
and started an attempt to upgrade and rationalize the Falcon's
computer systems. It was not quite finished when in a second game, Han
won the ship back. In a final game—which both men agreed would be
their last rematch over the Falcon—Lando won the ship back again, but
in a public attempt to impress Mara Jade, gave the Falcon back to Han.