20:27 is difficult for two reasons. One, the placement of this verse is difficult since it appears after the concluding sentences of chapter 20, 20:22-26, but also it is not part of the next chapter that begins with the phrase, "G-d spoke to Moshe," 21:1. A possible answer is that Devarim 18:11 refers again to this prohibition and Devarim 18:13 records that one is to be tamim with G-d. While the word tamim in Devarim 18:13 is understood to mean that a person should be whole with G-d and not turn to ghosts or spirits, still the literal idea of tamim is to be physically whole. In the following chapters in the book of Vayikra, 21:16-23 records the law of the priest who is physically blemished, and 22:19,21 refers to being tamim by the sacrifices, that one cannot offer a blemished sacrifice. Thus, maybe 20:27 is connected with the ensuing laws through the idea of being tamim by not turning to ghosts and spirits and that both the priests and the animals being sacrificed have to be physically whole.
A second difficulty in 20:27 is that this law of a person attempting to contact ghosts or spirits is mentioned twice within chapters 19,20. 19:31 records that one should not turn to ghosts or familiar spirits since they cause one to become tamei, and 20:6 records that if one turns to these ghosts or familiar spirits, then the person receives the punishment of karet. The fact that the Torah repeats this injunction in 20:27 is not surprising because people need to understand the severity of not turning to ghosts and spirits, as apparently many people believed (still?) in ghosts and spirits. However, why in 20:6 is the punishment for turning to ghosts and spirits karet, while in 20:27 it is death by stoning? Several answers have been suggested to answer this question.
Rashi (on 20:27) explains that 20:6, where the penalty is karet, is the case where the person willfully turned to ghosts or spirits, but was not warned about the prohibition. On the other hand, 20:27 is the case where the person turned to ghosts and spirits when he or she was warned and there were witnesses that saw the person violate this law. Similarly, Ibn Ezra (on 20:27) writes that 20:27 is dealing with the case where one turns to ghosts or spirits publicly.
Abravanel (on 20:27, 2005, p. 224) notes that 20:27 records the phrase “within themselves,” which leads him to suggest that 20:27 is referring to a person who claims that the ghost or spirit is within him or herself. I understand the Abravanel to mean that 20:6 differs from 20:27 that 20:6 (also 19:31) is the prohibition for a client, a person who goes to another person for that person to make contact with ghosts and spirits and who would then act based on the information “learned” from this contact, while 20:27 is referring to the “service provider,” of the client who (pretends) makes the actual “contact” with the ghost and spirits, and who does not necessarily act based on this “contact.” For example in the case of Shaul and the witch of Endor, Samuel I 28:3-25, Shaul was the client as he went to the witch, the “service provider” in order to use her “powers” so that he could speak to Shmuel’s ghost. According to this understanding of 20:6,27, Shaul would have violated 20:6 and the witch who made the contact would have violated 20:27. The difference in penalties in the verses is that 20:27 records the death penalty for those people who claim to make contact with ghosts and spirits since killing them would end the problem of people turning to ghosts and spirits since then there would be no more “service providers” (see also Shemot 20:27), while the penalty of karet would be for the clients. (Note, in this field of making contact with ghosts and spirits, there are many fewer people who are “service providers” than there are clients.) This understanding of 20:27 also suggests an answer to the first difficulty of 20:27.
20:27 is clearly connected to chapters 19,20 based on 19:31 and 20:6, but now we can suggest another reason how it can be related to the ensuing chapters. As suggested above, 20:27 is referring to the person who pretended to contact the ghosts and spirits and in many societies this person was (is?) frequently thought of as being a religious figure. The ensuing laws, 21:1-22:16 record laws relating to the priests. The priests are the legitimate religious figures in contrast to the people who claim to speak to ghosts and spirits who are illegitimate religious figures. Thus, 20:27 rules out the illegitimate religious figures, who are to be killed, and then chapter 21 continues with a discussion of the laws concerning the true religious figures. Similarly, Devarim 18:1-8 records laws about priests and Levites, Devarim 18:15-22 records the laws of the prophets, and sandwiched between these two sections, Devarim 18:9-14, records the prohibitions of magicians and people who turn to ghosts and spirits. Again, the Torah contrasts the legitimate religious figures with the illegitimate religious figures. Vayikra 20:27 would then be a connecting sentence between chapters 19 and 20 (also chapter 18 which is part of the same unit as chapters 19 and 20) and the following chapters, 21 and 22.
Bibliography:
Abravanel (Abarbanel), Yitzhak, (1437-1508), 2005, Commentary on Vayikra, Jerusalem: Horev.