WHEN DOES AVELUS BEGIN? [Avelus: beginning]
Gemara
(R. Avahu): A Mes knows everything that is said in front of him until Stimas ha'Golel.
(R. Chiya or R. Shimon b'Ribi): A Mes knows what is said in front of him until Stimas ha'Golel (this will be explained);
He learns from "v'Yashov he'Afar El ha'Aretz keshe'Hayah [veha'Ru'ach Tashuv El ha'Elokim]."
Mo'ed Katan 27a (Beraisa): From when do we invert the beds?
Answer #1 (R. Eliezer): It is from when [the Mes] leaves the house;
Answer #2 (R. Yehoshua): It is from Stimas ha'Golel.
Rishonim
Rambam (Hilchos Evel 1:2): The Chiyuv of Avelus begins from Stimas ha'Golel. There are no Isurei Avelus before burial. This is why David bathed and anointed when the baby (Bas Sheva's first son) died, before the burial.
Tosfos (Kesuvos 4b DH Ad): Rashi explains that Golel is the coffin cover, and Dofek are the boards on the side of the coffin. The Golel is Dofek (knocks) on them. The Aruch explains similarly. R. Tam asked that [Kohanim] used to jump over coffins (Berachos 19b). What was the Heter? The Golel and Dofek have Tum'as Ohel (Chulin 126a)! Also, we expound "Al Pnei ha'Sadeh" to include Golel and Dofek. According to Rashi, they are not on the face of the field! (They are below ground.) R. David of Natzinbork says that Rashi explains that the verse includes them when they separated [from the coffin]. As long as it is buried, it is Batel to the ground, and it has no Tum'as Ohel. This permitted jumping over coffins. Some explain that they were allowed to jump over and become Tamei, for Tum'as Ohel of a Golel does not obligate a Nazir to shave [and bring Korbanos Nazir Tamei and start Nezirus again from the beginning]. Such Tum'ah is not forbidden to a Kohen (Semachos 4:12). R. Tam says that this Beraisa is errant, for a Revi'is of blood does not obligate a Nazir to shave [and begin Nezirus again - Nazir 54a], but it is forbidden to a Kohen. Also, Eruvin 15b forbids using anything living for a Golel. It is not normal to make a coffin cover from a living being! In any case, the Gemara discusses several things that are not common. E.g. if the wombs of two animals were stuck together, and a fetus left one and entered the other (Chulin 70a). Even though it will never happen, we receive reward for learning the law.
Tosfos (ibid.): Rather, R. Tam explains that Golel is a monument put over a grave, i.e. a big rock. Dofek are two stones, one at each end of it. Sometimes, when the rock is not ready, they put an animal or something else there [temporarily] for a Siman. Kohanim used to jump over coffins without a Golel. The coffins themselves did not have Tum'as Ohel, for most of them have a hollow Tefach over the Mes.
Question: [If the Golel is a monument, it is not essential.] According to R. Yehoshua, when does Avelus begin when there is no Golel?
Answer (Tosfos): It is when those who buried him turn their faces away, like it says in Mo'ed Katan (22a) about those who do not follow the Mes [to the cemetery].
Rosh (Mo'ed Katan 3:39): Once they gave the Mes over to those who will take it, and turned away, they despaired, so this is like Stimas ha'Golel. If it will be buried in a cemetery near the city gate, those who return from the gate are not Mis'avel until they are told that he was buried. Their minds are on the Mes until then. It is as if he is in front of them. It is as if he was given to those who carry the Mes on their shoulders. R. Tam and R. Chananel say that Golel is a rock to cover a grave. Once the grave is filled, Avelus begins. However, he does not remove his shoes until reaching the house. Even if he removed them, if he must walk on the road, he puts on his shoes until reaching the house. The custom in Ashkenaz is to remove shoes immediately for a parent and walk barefoot to the house. Evel Rabsi (4:10/7) says that [a Kohen] is Mitamei for his relatives until Stimas ha'Golel. If this were in the house, one cannot say that he is not Mitamei for one's father after this! Rather, it is sealing the grave. When R. Shimon ben Yehotzadak died, his brother came from Galil after Stimas ha'Golel. Chachamim ruled that he may not become Tamei, but they will open half the grave and he will look. Once, a youth wrote his property to others, and died. His family wanted to check the body [for signs of adulthood]. Chachamim said that we do not move him from his place after Stimas ha'Golel (Evel Rabsi ibid.) All this shows that Stimas ha'Golel is after burial and sealing the grave with earth.
Poskim
Shulchan Aruch (YD 375:1): Avelus begins immediately after burial, when the grave is filled with earth.
Beis Yosef (DH v'Chasav): Rashi explains that Golel is the coffin cover. The Ramban said that not everyone is buried in a coffin. Even so, sealing the grave is called Golel. Rashi teaches that if they put the Mes in a coffin and sealed it in the house, Avelus begins from then. Rashi agrees that if they move the Mes from place to place in a coffin, and they will remove him for burial, Avelus starts from when the grave is sealed. Rashi's primary Perush is that covering the coffin is called Golel. If they seal it with nails with intent not to open it again, whether in the house or in the cemetery, it is as if he is in the grave. According to this, if the take the Mes to the cemetery in a bed or open coffin, Shivah begins from when the grave is sealed with earth. If they put the Mes in a coffin and nailed it closed, with intent to put the coffin in [a prepared hollow] in a burial cave or in a grave, he counts from when they sealed it.
Beis Yosef (DH v'Chasav Od): The Ramban said that the Tosafists, Aruch and R. Chananel say that Golel is a big rock put on the grave to seal it, but not for a monument. He says that the Ge'onim disagree with Rashi, and say that one does not count until the coffin is put in the hollow or grave and sealed with rocks or boards, according to the custom. It seems that the Rambam holds like R. Tam and R. Chananel. We hold like them.
Bach (2): The Mordechai (Mo'ed Katan 900) says that when R. Klonimus died amidst a siege, they put him in a coffin and started counting Shivah, even though they planned to take him [for burial] after the siege will end. According to the Ramban, we must say that they held that Rashi's opinion is primary. R. Tam and the Ge'onim do not count as long as the coffin is open. The Shulchan Aruch rules like R. Tam in Sa'if 1. He contradicts himself in Sa'if 4! There, he brings the episode with R. Klonimus, and says that sealing the coffin is like burial, and Avelus begins immediately. Really, Rashi's Perush is primary. This is clear from the Mordechai, who did not even bring R. Tam. Also, what was done in practice is primary. Or Zaru'a answered all the questions against Rashi, and says that his opinion is primary. Also Tosfos in Kesuvos answered the questions against Rashi. There and in Shabbos (152b DH Ad) he concludes like Rashi. Elsewhere, Tosfos merely explained R. Tam, but he did reveal how he rules. This is the Halachah. The custom is to be stringent not to count Avelus until the grave is sealed, for according to Rashi one must be meticulous to see if the coffin was sealed with intent to open it. Also, most Mesim are taken for burial in open coffins.
Shach (373:11): Rashi says that Golel is covering the coffin. I say that he does not mean even if this was in the house, rather, when they cover it in the grave. We say that a Mes knows what is said in front of him until Stimas ha'Golel, and we learn from "v'Yashov he'Afar El ha'Aretz", i.e. the body returns to the earth!
Gra (1): This (filling the grave with earth) is called Stimas ha'Golel. Evel Rabsi and several places in the Gemara connote like this. Shochar Tov (Medrash Tehilim 16, DH l'Kach) says that the Avos were put in the ground and the Golel was sealed in front of them.
Note: Some texts of the Medrash say "... until they were put in the ground... R. Pinchas says... until they died and the Golel was sealed in front of them."
Shulchan Aruch (ibid.): He covers his head, but does not remove his shoes until reaching his house. Nowadays the custom is to remove the shoes immediately after Stimas ha'Golel, like it says in 376:4.
Gra (3): Ta'anis 13a says so, and also the Yerushalmi and Tosefta.