Reb Chaim Brisker explains that the custom of hiding the Afikoman has its roots in Halachah: eating the afikoman as the last item of the meal commemorates the Korban Pesach which was the last to be eaten in the meal (הרא”ש).
The law by the Korban Pesach was that they needed to eat in purity, for this reason they would hide it, thus ensuring that nothing impure touched it. It is for this reason that we to commemorate their act and hide the Afikoman as well.
(הגר”ח, מו”ז ח”ז סי’ קפח)