When Yaakov Avinu initially refuses adamantly to allow his youngest son Binyamin to go down to Egypt with his brothers, he expresses the fear that something terrible will happen to Binyamin along the way (בראשית מ"ב:ל"ח). The Yerushalmi in Shabbos (פרק ב' הלכה ו', דף י"ט:) presents the question of why Yaakov was so worried about a disaster happening specifically while Binyamin would be travelling, when in truth, a disaster could happen just as easily when he would be at home, if that is what Hashem wills. The answer suggested (שם) is that the שטן, that is, the prosecuting angel, somehow protests more vigorously and more successfully about a person when the person already is in a state of danger; when a person has, on his own, placed himself in a dangerous position, there is thus a far greater likelihood that a disaster will befall him, because the שטן may be able to somehow "convince" Hashem to act then against this person. The Yerushalmi (שם) also quotes other Pesukim which document this idea that when one is in a situation of danger, there is a greater likelihood of a tragedy taking place; apparently, the Yerushalmi (שם) considers being out on the road on a journey to be a situation involving some danger, and this is why Yaakov was particularly concerned about something happening to Binyamin while he would be out on the road. The Yerushalmi in Berachos (פרק ד' הלכה ד', דף ל"ד:) states similarly that all roads are considered to be dangerous places, and the Shulchan Aruch (אורח חיים סימן רי"ט סעיף ז') cites this ruling.
Perhaps with this idea in mind, the Gemara in Berachos (דף כ"ט:) indicates that whenever one goes out on the road to take a trip, he must recite a special prayer called Tefillas HaDerech; the idea of such a Tefillah is actually reported (םש) as something told by Eliyahu HaNavi to a certain Amora. The Gemara (םש) then presents the text of this Tefillah, which includes the requests that Hashem should guide the person along his way in peace and protect him from any trouble or danger along the way, as well as that he should help him return to his home in peace, as noted by the Vilna Gaon in his emendation to the text there (הגהות הגר"א שם אות ג'), based possibly on the version of this Tefillah presented by the Rosh there in Berachos (ד' סימן י"ח פרק), and concludes with the Beracha of שומע תפלה, indicating that Hashem hears our prayers. The Gemara (שם ובדף ל. שם) then adds that even if one is travelling alone, one should recite this Tefillah using the plural forms in order to include the broader Jewish community in one's prayers. The Semag ( י"טמצות עשה) lists this obligation to recite Tefillas HaDerech as one of the many Halachos included in the general Mitzvah to daven to Hashem daily, as does the Semak (סימן י"א); both these authorities also include a text similar to that presented in the Gemara (שם).
Interestingly, however, the Rambam makes no mention at all of this Tefillah; in his commentary on the Tur (או"ח סימן ק"י), who does cite the obligation to recite Tefillas HaDerech and presents his version of its text, the Beis Yosef (שם בד"ה והרמב"ם) wonders why the Rambam omitted this entirely, and leaves it as a question. The Rambam (פרק י' מהל' ברכות הלכה כ"ה) actually does present the text of a Tefillah that one should say prior to entering a city, requesting that he be able to enter in peace, and one which should be said after entering a city, thanking Hashem for enabling him to enter in peace. He also mentions (םש) a Tefillah to be recited before leaving a city, requesting the ability to be able to leave in peace, and one to be recited after leaving, thanking Hashem for enabling him to leave in peace, and requesting a peaceful and safe journey; this latter Tefillah, cited by the Beis Yosef (םש), sounds like Tefillas HaDerech in terms of its content. These Tefillos, however, seem to be based on a Mishnah elsewhere in Berachos (דף נ"ד.), as interpreted by the Gemara later there (שם דף ס.), and significantly, as the Beis Yosef (םש) stresses, the Rambam (םש) says nothing about a Beracha as part of their texts, which the standard Tefillas HaDerech, as noted above, includes.
The Shulchan Aruch (או"ח סימן ר"ל סעיף א'), in codifying these other Tefillos, clearly links them to Tefillas HaDerech, stating that a Beracha is part of the text, a position he elaborates upon in the Beis Yosef on the Tur (שם בד"ה המתפלל), where the Kol Bo (סימן פ"ז) is cited as also making this connection and mentioning a Beracha. The Bach, however, in his commentary on the Tur (שם בד"ה המתפלל), clearly writes that these Tefillos are not really related to Tefillas HaDerech, and the Rambam himself, in his Peirush HaMishnayos in Berachos (פרק ט' משנה ד'), states that these prayers are not really Tefillos or Berachos in the true sense, but rather are merely requests. The Aruch HaShulchan (או"ח שם סעיף ד') notes that this may be one reason why we are not accustomed to saying these Tefillos today; it thus remains difficult to understand why the Rambam makes no mention, or certainly no direct mention, of Tefillas HaDerech. The Pri Chodosh (שם סימן ק"י ס"ק ד') attempts to explain the Rambam's position, suggesting that there is a difference regarding the Beracha which concludes Tefillas HaDerech, as well as regarding other particulars about the text, depending upon the level of danger in the place where the person is travelling, and he thereby reinterprets the original Gemara in Berachos (דף כ"ט:), but the Kaf HaChaim (שם אות י"ג) notes that this interpretation has not been accepted by the overwhelming majority of the Poskim, although the Mishnah Berurah (שם סימן ר"ל ס"ק ג') implies something similar in his attempt to understand the relationship between the different Tefillos cited above and Tefillas HaDerech. The Aruch HaShulchan (שם סימן ק"י סעיף ט') is also troubled by the Rambam's omission of any direct reference to Tefillas HaDerech and to a concluding Beracha; he attempts to read in to the words of the Rambam (םש) regarding these other Tefillos, but ultimately leaves the matter as a question, noting that the Rambam entirely omitted even a hint at several important Halachos about Tefillas HaDerech.
The Shulchan Aruch (או"ח שם סעיף ד'), however, does indeed rule that one who goes on a trip must recite Tefillas HaDerech, and he adds (םש) that it is preferable that one recite this Tefillah standing still, as indicated by one authority in the Gemara in Berachos (דף ל.), although, as the Mishnah Berurah (שם ס"ק כ"ב) and the Kaf HaChaim (שם אות מ"ג) point out, if it is not possible to stand still, such as if the person will become bothered by having to stop and stand, he may then recite it while moving or while sitting down. The Shulchan Aruch (םש) also states that if one is riding, he need not stop and get off in order to recite Tefillas HaDerech; the Mishnah Berurah (שם ס"ק כ"ג) adds that this applies whether one is riding on an animal or on a wagon, but that it is preferable to stop the animal(s) for the moment that it takes to actually recite the Tefillah, if this is possible, as the Magen Avraham (שם ס"ק י"א) recommends. The Kaf HaChaim (שם אות מ"ה) rules this way as well, noting that if one can stand up in the wagon, he should do so, since standing for this Tefillah is preferable, as documented above. All of these same rules would presumably apply to one who is in a car or a bus or some other modern form of transportation; the Aruch HaShulchan (שם סעיף י"א) makes specific reference to travelling by train or by ship, stating that one can easily stand in those cases, and adding that one may recite one's own additional Tefillos in such cases, if one wants to. Rav Ovadyah Yosef (שו"ת יחוה דעת חלק ב' סימן כ"ו) seems to believe that this Tefillah must be recited as well when one travels in an airplane, although he notes that there is some question about this; the same conclusion may be inferred from a Teshuvah by Rav Moshe Feinstein(.)שו"ת אגרות משה או"ח חלק ב' סימן כ"ט The Gemara in Berachos (םש) states that one should recite this Tefillah when one begins one's journey; the Shulchan Aruch (שם סעיף ז') rules accordingly. The Magen Avraham (שם ס"ק י"ד), however, states that one should not recite Tefillas HaDerech until one has left one's town and is beyond even the city limits, which extend, according to Halacha, a little more than 70 Amos (about 105-140 feet) beyond the last houses in the town, because that area is still considered part of the town; the Taz (שם ס"ק ז'), though, writes that one may say it even in town before one has embarked on the trip, as long as one has decided that he is definitely going. The Mishnah Berurah (שם ס"ק כ"ט) cites this dispute and asserts that most Poskim disagree with the Taz (שם) and say that one should wait until one is on one's way, although one may rely on that view if need be; he then notes (םש), however, citing the Pri Megadim (שם ס"ק י"ד באשל אברהם), that this requirement to wait before saying Tefillas HaDerech is true only when one first sets out on one's journey. But if one is taking a journey which lasts for several days, staying overnight on the road, in which case he must say Tefillas HaDerech each day that he travels, as stated by the Taz (שם ס"ק ה') and the Mishnah Berurah (שם ס"ק כ"ד), among others, he may say Tefillas HaDerech on any subsequent days as soon as he prepares to depart, even if he is still actually within the city limits. The Kaf HaChaim (שם אות נ"ב) rules, however, that if one is particularly frightened or nervous about travelling, he may recite Tefillas HaDerech even before he leaves the city limits on any day of any trip.
The Gemara in Berachos (םש) also indicates that the distance one must be travelling in order to become obligated to recite Tefillas HaDerech is at least one פרסה, which is about three miles. The Shulchan Aruch (םש) rules accordingly, and the Taz (שם ס"ק ו') and the Mishnah Berurah (שם ס"ק ל') explain that travelling less than that distance is like travelling within the city where there is no danger; if, however, one knows that there is danger along a particular route, one should say Tefillas HaDerech in any case, even if the distance is less than a פרסה. The Aruch HaShulchan (שם סעיף ט"ו) concurs with this, and the Kaf HaChaim (שם אות כ"ו) cites many others who do as well. As a side point, Rabbeinu Yonah in Berachos (דף כ.-כ: בדפי הרי"ף בד"ה משעה) explains in this connection that the Yerushalmi in Berachos cited above (םש) which labels all roads as dangerous does not refer to roads near the city.
Rav Ovadyah Yosef elsewhere (שו"ת יביע אומר חלק א' חאו"ח סימן י"ג, ובחלק ב' חאו"ח סימן י"ד), however, questions whether this minimum travel distance of a פרסה is finite, meaning that it doesn't matter whether one is going by foot or by train, and that either way one becomes obligated to recite Tefillas HaDerech after travelling for one פרסה, regardless of how much time that takes, or whether that distance mentioned in the Gemara (םש) was intended to represent a certain amount of time on the clock, meaning that one must travel for as long as one would take to walk an entire פרסה in order to be obligated to recite Tefillas HaDerech, regardless of the actual distance one may have covered. He concludes (שם ושם), after pointing out that this may be a dispute among various Acharonim, that the latter view is correct, saying that since it takes 72 minutes to travel a distance of one פרסה (by foot) according to the Halacha, that is the amount of time which must elapse on one's journey before any obligation to recite anything sets in, irrespective of how far one may actually travel during that time. Although Rav Ovadyah Yosef in these Teshuvos (שם ושם) is discussing the obligation to recite Birchas HaGomel after a successful and safe journey, he states clearly in another Teshuvah (שם חלק ו' חאו"ח סימן מ"ח) that the same ideas apply to Tefillas HaDerech; if one is travelling for less than 72 minutes, then, one would not be required to say Tefillas HaDerech. It should be pointed out that the period of 72 minutes is arrived at here based on the fact that, as the Mishnah Berurah (שם ס"ק ל"א) notes, one פרסה is equal to four מיל, and as the Mishnah Berurah explains elsewhere (שם סימן תנ"ט ס"ק ט"ו, ועיין שם בביאור הלכה בד"ה הוי), one מיל is covered in 18 minutes. It should also be noted, as the Ramo states (שם סימן ק"י סעיף ז'), that even if one is travelling for more than the distance or the time represented by a פרסה, it is still preferable to recite Tefillas HaDerech within the first פרסה, although if one forgets to do so, he may say it later on in his trip.
It is interesting to note that the text of Tefillas HaDerech does not begin with the language usually used to start a Beracha (...ברוך אתה ה') as most other Berachos do, although, as stated above, it concludes with that language. According to Tosafos in Pesachim (דף ק"ד: בד"ה כל), this is because this Tefillah is not really a Beracha, but a general Tefillah and a request for mercy, while according to Rabbeinu Yonah in Berachos (דף א. בדפי הרי"ף בד"ה אלא), this is because the Beracha of שומע תפלה, which concludes Tefillas HaDerech, generally is associated with other Berachos (in the Shemoneh Esrei), and thus does not need to begin with the phrase "ברוך אתה ה'..."; as a result, it is recited without that opening phrase in Tefillas HaDerech as well. The Tur (או"ח שם) also discusses this issue, and says that it was the practice of the Maharam of Rothenburg to recite Tefillas HaDerech immediately after reciting some other Beracha so that it would have the phrase "ברוך אתה ה'..." associated with its opening; the Shulchan Aruch (שם סעיף ו') cites this as well. The Magen Avraham (שם ס"ק י"ג) recommends that one follow this practice, such as by eating something right before reciting Tefillas HaDerech, in order to relate it to the Beracha over the food; the Aruch HaShulchan (שם סעיף י"ד) and the Kaf HaChaim quote this as well, as does the Mishnah Berurah (שם ס"ק כ"ח) who adds that if this is not possible, one must still recite Tefillas HaDerech.