The Torah prohibits owning (Shemot 12:19) and seeing (Shemot 13:7) Hametzduring Pesah. As such, throughout the generations, people would either give away their Hametz to non-Jews or would simply discard it before the holiday. Since discarding one’s Hametz would represent a sizable financial loss, the Tosefta (Pesahim 1:6) discusses selling one’s Hametz to a non-Jew. Indeed, the Shulhan Aruch (Orah Haim 448:3) codifies this by saying that one may either sell one’sHametz to a non-Jew, or it give it away for free as a gift. It became common for people to appoint the community rabbi to be their agent and to sell that community’s Hametz to a designated non-Jew. In cases where the value of the community’s Hametz was significant to the point that the non-Jew could not pay the total cost, a down-payment would be advanced and the non-Jew would have the duration of the holiday to pay off the balance.
Rabbi Yosef Messas (Otzar HaMichtavim, vol. 3, § 9) explains that historically the selling of Hametz was would occur between Jews and one non-Jews on an individual basis, without appointing a rabbi. This eventually became impractical so people found it more convenient to sell their Hametz directly to the rabbi. This too was eventually replaced with the custom of the rabbi acting as an agent and selling the Hametz to a non-Jew on behalf of the community.
When this was done on a small scale, Rabbi Yosef Messas (ibid.) and Rabbi David Ovadia (Nahagu Ha’am) said that it was preferable for individuals to hand over their keys to the rabbi so that the non-Jew would theoretically have access to the Hametz. Nowadays, when the value of a community’s Hametz is significant, Rabbi Haim Medini (Sde Hemed, Ma’arechet Hametz Umatza, § 9) says that since the non-Jew does not necessarily pay for the Hametz in full, one may be lenient and not give one’s house keys to the rabbi. Rabbi Ben Zion Abba Shaul (Or LeZion, vol. 3) takes the strict approach that must hand over one’s keys to the rabbi who is selling Hametz on one’s behalf, however there is room to be lenient.
Summary: Selling Hametz to a non-Jew is a bona-fide legal transaction. The non-Jew is entitled to pay in full and keep the Hametz, or it may be sold back to the Jew.