The Chachamim teach (Berachot 60b), “One who goes to sleep in bed must recite Shema Yisrael until V’Hayah Im Shamo’a, and the blessing Baruch HaMapil Chevlei Sheinah….” Likewise, Rabbi Yehoshua ben Levi says, “Even if a person recited Keriat Shema in the synagogue, it is a mitzvah to recite it [again] on his bed.” Chazal base their words on the verse (Psalms 4:5), “Meditate in your hearts [while] on your beds and be silent” (Berachot 4b).
Rabbi Yehoshua ben Levi was accustomed to reciting an additional two Psalms before his sleep: “Yoshev B’Seter Elyon” (Psalm 91) and “Hashem Ma Rabu Tzarai” (Psalm 3), which are useful in warding off the evil spirits (Shevuot 15b), and many follow his minhag. Further, throughout the generations, people became accustomed to adding on Psalms, verses, and prayers. Since these are late additions, there are differences between the nusachim of the various ethnic groups. Some say that it is good to recite the paragraph of V’Hayah Im Shamo’a as well, and others maintain that it is best to recite all three paragraphs.
To summarize, in accordance with the enactment of the Chachamim, it is necessary to recite the paragraph of Shema and Birkat HaMapil before one goes to sleep, but the remaining Psalms are not an obligation. The minhag to recite them became widespread because of the custom of Rabbi Yehoshua ben Levi (see Magen Avraham 239:2).
There are those who are strict to recite Birkat HaMapil immediately before they sleep, after reciting Shema and all the other verses. However, according to the custom of the kabbalists, Birkat HaMapil precedes Shema and the other verses. One who is concerned that he will fall asleep while reciting the prayers or before concluding them should start with Shema and HaMapil, thereby ensuring that before he falls asleep he recites the passages instituted by Chazal (see Mishnah Berurah 239:2).
. The Chachamim instituted only the recital of the Shema paragraph before going to sleep, as the Rif (3:1), Rambam, and the Rosh (chapter 1, 6) write. However, in chapter 9, 23, the Rosh brings the opinion of Rabbeinu Chananel who maintains that V’Hayah Im Shamo’a is recited as well. Perhaps Rabbeinu Chananel understood the Gemara’s words “until V’Hayah Im Shamo’a” to mean, including V’Hayah Im Shamo’a. Divrei Chamudot 67 interprets that he is referring to a place in which Ma’ariv is recited before tzeit hakochavim, for there, in order to fulfill the mitzvah of Keriat Shema, it is necessary to recite both paragraphs. Rabbeinu Yerucham (netiv 3, part 2) and Rabbeinu Yonah in Sefer HaYir’ah write that all three paragraphs are recited, for together they contain 248 words, and saying them provides a special protection against harmful forces.