In order to answer this question, a certain background into the nature of the prohibition to imitate Gentile customs must be understood. Tosafot [a medieval Talmud commentary] understands that two distinctly different types of customs are forbidden by the prohibition of imitating Gentile customs found in Leviticus 18:3. The first is idolatrous customs and the second is foolish customs found in the Gentile community, even if their origins are not idolatrous.
Rabbenu Nissim (Ran) and Maharik disagree and rule that only customs that have a basis in idolatrous practices are prohibited. Apparently foolish -- but secular -- customs are permissible so long as they have a reasonable explanation (and are not immodest). Normative halakhah follows the ruling of the Ran and Maharik. As noted by Rama [Rabbi Moshe Isserles, c. 1525-1572]:
Those practices done as a [Gentile] custom or law with no reason one suspects that it in an idolatrous practice or that there is a taint of idolatrous origins; however, those customs which are practiced for a reason, such as the physician who wears a special garment to identify him as a doctor, can be done; the same is true for any custom done out of honor or any other reason is permissible.
Rabbi Isserless is thus clearly prohibiting observing customs that have pagan origins, or even which might have pagan origins. His opinion, the most lenient found in normative halakhah, is the one we follow.
Of course, independent of the halakhic obligation to avoid Gentile religious customs, Jewish law forbids a Jew from actually celebrating idolatrous religious events himself.
Based on this, in order to justify candy collection on Halloween, one would have to accept the truthfulness of any of the following assertions:
1) Halloween celebrations have a secular origin.
2) The conduct of the individuals "celebrating Halloween" can be rationally explained independent of Halloween.
3) The pagan origins of Halloween or the Catholic response to it are so deeply hidden that they have disappeared, and the celebrations con be attributed to some secular source or reason
4) The activities memorialized by Halloween are actually consistent with the Jewish tradition.
I believe that none of these statements is true.
Applying these halakhic rules to Halloween leads to the conclusion that participation in Halloween celebrations -- which is what collecting candy is when one is wearing a costume -- is prohibited. Halloween, since it has its origins in a pagan practice, and lacks any overt rational reason for its celebration other than its pagan origins or the Catholic response to it, is governed by the statement of Rabbi Isserles that such conduct is prohibited as its origins taint it. One should not send one's children out to trick-or-treat on Halloween, or otherwise celebrate the holiday.
The question of whether one can give out candy to people who come to the door is a different one, as there are significant reasons based on darkhei shalom (the ways of peace), eva (the creation of unneeded hatred toward the Jewish people), and other secondary rationales that allow one to distribute candy to people who will be insulted or angry if no candy is given. This is even more so true when the community -- Jewish and Gentile -- are unaware of the halakhic problems associated with the conduct, and the common practice even within many Jewish communities is to "celebrate" the holiday. Thus, one may give candy to children who come to one's house to "trick or treat" if one feels that this is necessary.
Rabbi Michael Broyde is an associate professor of law at Emory University and the rabbi of the Young Israel of Toco Hills, Atlanta.