The Gemara in Berakhot 17b lays the foundational structure: those who carry the bier and those who walk before it — provided their presence is needed — are exempt from prayer, while those who walk behind it and are not needed remain obligated; and after the burial, those returning are guided by whether they can complete Shema before reaching the row of consolers.
Berakhot 18a establishes why accompanying the deceased matters in the first place: Rav Yehuda teaches that one who sees a deceased being taken to burial and does not escort him transgresses the verse 'he who mocks the poor blasphemes his Creator' (Mishlei 17:5), while one who does escort him is rewarded as though lending to God.
The question of who walks before and after the bier is treated in the Ramban (Torat HaAdam, Inyan HaHotza'ah), which records the Talmudic dispute: the custom varied by locale — women either before or after the bier — with R. Yehuda holding women always go before, citing David walking behind Avner's bier, while the Yerushalmi offers two competing rationales: one holding women go first because they brought death into the world, the other that men go first for the honor of Jewish women so men not gaze upon them.
Mishnah Sanhedrin 2:3 likewise invokes David's walking behind Avner's bier, but the Sages respond that David did so only to appease the people, not as a normative precedent — an exchange that grounds the broader discussion of who properly follows the mitah.
The Shulchan Arukh Shulchan Arukh, Yoreh De'ah 358:3 records both the order of participants — in some communities the mourners exit first, then relatives, then the bier is placed on their shoulders, followed by the broader community — and the practice of halting with the deceased every four cubits on arriving at the cemetery before burial; it further specifies that two or three such halts are made before reciting Tzidduk HaDin, and that on days when Tzidduk HaDin is not said, the halts are not required.
Arukh HaShulchan, Yoreh De'ah 358 parallels and reinforces these rulings, recording the same order of mourners, relatives, and community taking up the bier in sequence, and the same custom of halting multiple times before Tzidduk HaDin.
Kitzur Shulchan Arukh 197:8 adds a distinct rule for leaving the house: care must be taken that no one walk out ahead of the deceased, with the sole exception of the pallbearers who must exit first in order to carry him out.
Minchas Elazar (Chelek 4, 2) cites the Kol Bo to the same effect regarding the order of carrying — the washers bring the deceased out to the threshold, whereupon the mourners and close relatives take him on their shoulders first, and only then the rest of the community — and reasons that the closer the relative, the greater the priority to carry and accompany.
Upon returning from the beis hachaim, Shulchan Arukh, Yoreh De'ah 376:4 records the full sequence of post-burial practice: shoes are removed, Kaddish (the 'Yehei Shmei Raba' of the world-to-come formula) is recited, earth and grass are pulled and cast behind the back, and hands are washed; the Rema adds that some communities sit seven times on account of spirits that accompany the mourner, while in Ashkenazic practice the custom is to sit three times, each time reciting 'Yehi Noam' and 'Yoshev Beseter,' with this practice applicable even when burial occurs on Yom Tov or near Shabbat.
Mishneh Torah, Mourning 12:1–4 addresses the rite of ma'amad u'moshav — the standing and sitting no fewer than seven times at the cemetery on the first day in communities where it is practiced — which is conducted before relatives who are not primary mourners, with dirges recited while they stand and sit alternately at the direction of the one leading the rite.