OPINIONS: The Gemara quotes a Beraisa which says that the only difference between the Shemoneh Esreh recited on a Ta'anis by an individual and the Shemoneh Esreh recited by the Shali'ach Tzibur is that the individual's Shemoneh Esreh contains 18 blessings and the Shali'ach Tzibur's Shemoneh Esreh contains 19 (because of the additional blessing of "Aneinu").
RASHI asks why the Gemara refers to the ordinary Shemoneh Esreh of an individual as having only 18 blessings when it actually has 19 blessings (even without the blessing of "Aneinu").
(a) RASHI answers that the title of the prayer, "Shemoneh Esreh," refers to the 18 blessings which it contained when it was originally composed. The blessing of "v'la'Malshinim" was instituted later by the sages in Yavneh (Berachos 28b).
(b) The TOSFOS RID disagrees with Rashi. He explains that the reason why the Beraisa says that the ordinary Shemoneh Esreh contains 18 blessings is because the Beraisa maintains that there indeed are only 18 blessings in the Shemoneh Esreh. He cites a Tosefta in Berachos (end of chapter 3) which states that the 18 blessings include the blessing of "v'la'Malshinim" (which mentions the downfall of the Minim and the Posh'im). The blessing of "Boneh Yerushalayim" (which mentions the rebuilding of Yerushalayim) and the blessing of "Es Tzemach David" (which mentions the restoration of the dynasty of David ha'Melech) are merged into a single blessing. The Tosefta concludes that if one recites two separate blessings, one in which he mentions David ha'Melech and the other in which he mentions the rebuilding of Yerushalayim, he fulfills his obligation to recite Shemoneh Esreh. This implies that the Tosefta maintains that l'Chatchilah one should include "Boneh Yerushalayim" and "Es Tzemach David" in a single blessing, and thus, according to the Tosefta, the Shemoneh Esreh indeed contains only 18 blessings even after the addition of the blessing of "v'la'Malshinim."
The view of the Tosefta is reflected in the Piyutim, the additional prayers composed for the Shali'ach Tzibur to recite during each blessing of Shemoneh Esreh on festivals and fasts, and which reflect the theme of each blessing. There are consistently only 18 of these additional prayers; the Piyut which mentions the rebuilding of Yerushalayim is always the same as the one which mentions the restoration of the dynasty of David ha'Melech.
The Tosfos Rid points out that this opinion was also the practice of the Yerushalmi (Berachos 4:5, Rosh Hashanah 4:6). The Yerushalmi mentions that the Chasimah (closing blessing) of one of the blessings of Shemoneh Esreh is "Baruch Atah Hash-m Elokei David u'Voneh Yerushalayim" -- it includes David ha'Melech and Binyan Yerushalayim in one blessing. This is what the Beraisa means when it says that the ordinary Shemoneh Esreh contains only 18 blessings.
The Bavli, however, clearly counts "Boneh Yerushalayim" and "Es Tzemach David" as two separate blessings (Megilah 17b). In addition, the Gemara in Sanhedrin (107a) relates that David ha'Melech asked Hash-m that a mention of "Elokei David" be included in the Shemoneh Esreh, like "Elokei Avraham." The Gemara says that Hash-m did not acquiesce to David's request. Accordingly, the Bavli is consistent with its view that the blessing of Boneh Yerushalayim does not include the words "Elokei David," and instead a separate blessing of "Es Tzemach David" is recited. (The Tosefta itself says that if one recites separate blessings for David ha'Melech and Yerushalayim he does not need to repeat the Shemoneh Esreh, and this apparently was the practice adopted l'Chatchilah in Bavel.)
The practice today follows the view of the Bavli. Rebbi Elazar ha'Kalir and the other authors of the Piyutim lived in Eretz Yisrael and followed the practice of the Yerushalmi, and thus they wrote Piyutim for only 18 blessings and merged the prayer for Yerushalayim and the prayer for David ha'Melech into a single blessing.
Although no Jewish community today follows the practice to combine the prayer for Binyan Yerushalayim with the prayer for David ha'Melech. Even in contemporary Sidurim remnants of the early practice can be found. In the end of the blessing of Boneh Yerushalayim, the words "v'Chisei David..." are said. This phrase is probably a remnant of the original practice to conclude the blessing with the words, "Elokei David u'Voneh Yerushalayim," since the end of the blessing (before the Chasimah) must reflect the words recited in the Chasimah. RAV YEHUDAH LANDY adds that this also explains why the blessing of Boneh Yerushalayim begins with a Vav - "v'li'Yerushalayim Ircha..." -- the Vav ("and") indicates that this blessing was added to the Shemoneh Esreh when the single blessing for Yerushalayim and Malchus Beis David were separated into two. (Perhaps "v'la'Malshinim" begins with a Vav for a similar reason. The Vav indicates that it was added later and was not part of the original Shemoneh Esreh.)
The Tosfos Rid implies that the original practice, before the addition of the blessing of "v'la'Malshinim," was to recite only 17 blessings in the Shemoneh Esreh. When the additional blessing of "v'la'Malshinim" was instituted in Yavneh it brought the total to 18, and not 19, blessings. Indeed, the Midrash states this explicitly (Bamidbar Rabah 18:21, and Tanchuma, end of Parshas Korach; see also Midrash Tehilim 17:4). The Midrash says that the number of blessings in the Shemoneh Esreh is equal to the Gematriya of the word "Tov" (17). Even though our Shemoneh Esreh contains 19 blessings, the original Shemoneh Esreh contained only 17 blessings because "v'la'Malshinim" and "Es Tzemach David" (or "v'li'Yerushalayim") were later additions. According to the Midrash, the blessing of "Es Tzemach" was added even later than "v'la'Malshinim."
(The Midrash seems to disagree with the Gemara in Berachos (28b) which states that the blessing added in Yavneh was the nineteenth blessing. It seems that the original enactment of Shemoneh Esreh included only 17 obligatory blessings as well as an option to split "Boneh Yerushalayim" and "Es Tzemach" into two blessings (as the Tosefta clearly permits). Consequently, both the Midrash and the Gemara are correct: In Yavneh the eighteenth blessing was added, but that blessing could be viewed as the nineteenth because a person was entitled to divide "Boneh Yerushalayim" and "Es Tzemach" into two blessings. It is interesting to note that according to a common Girsa in the Yerushalmi's version of how "v'la'Malshinim" was added in Yavneh (Berachos 4:3 and Ta'anis 2:2), the Yerushalmi cites the story to explain why there are 18 -- and not just 17 -- blessings.)
The Bavli, which says that the words "Elokei David" are not recited in the Shemoneh Esreh, implies that it was never the practice to mention "Elokei David." Apparently, before the blessings of "Boneh Yerushalayim" and "Es Tzemach" were split, the Chasimah was "Magen David vi'Yerushalayim," and not "Elokei David u'Voneh Yerushalayim." When the people of Bavel divided the blessing into two, the people of Eretz Yisrael correspondingly gave the single blessing a double ending, thereby granting David ha'Melech special status by mentioning him separately in the Chasimah. The people of Bavel did not accept this practice for two reasons. First, the Gemara says that "Elokei David" should not be said in the Shemoneh Esreh. Second, the Gemara (Berachos 49a) says that two subjects should not be included in the Chasimah of a single blessing.
RAV DAVID COHEN shlit'a (in a special section at the end of OHEL DAVID, vol. 2) uses this approach to explain the words of TOSFOS in Megilah (17b, DH v'David). Tosfos implies that Rashi had a tradition to count chapters 9 and 10 of Tehilim as one chapter. (Indeed, there is a strong contextual connection between the two chapters, which implies that they should be connected: In chapter 9, every other verse starts with a consecutive letter of the Hebrew alphabet, but reaches only until the letter Kaf. The first verse in chapter 10 starts with the letter Lamed, and the final alternating verses of the chapter begin with the letters Kuf, Reish, Shin, and Taf.) Why, then, in contemporary books of Tehilim are they divided into two separate chapters?
Rav David Cohen explains that they originally were one chapter and later the Chachamim divided them into two chapters. The Gemara in Berachos (9b) says that, originally, chapters 1 and 2 were one chapter. The MAHARSHA there explains that the first 18 blessings of Shemoneh Esreh were instituted to correspond to the first 18 chapters of Tehilim. The Shemoneh Esreh concludes with the verse from Tehilim, "Yiheyu l'Ratzon Imrei Fi...." When the Chachamim added a new blessing in the Shemoneh Esreh (bringing the total to 19 blessings), they decided to add a new chapter number in Tehilim so that the verse of "Yehiyu l'Ratzon" would appear after 19 chapters, and therefore they divided the first chapter into two.
Similarly, chapters 9 and 10 were originally one chapter. However, after the Chachamim added the blessing of "Es Tzemach David" to the Shemoneh Esreh, they wanted to add a new chapter so that "Yiheyu l'Ratzon" would still appear in Tehilim after the corresponding number of blessings in the Shemoneh Esreh, and therefore they split another chapter of Tehilim into two. (He points out that the contents of Psalms 2 and 10 correspond to the contents of these two blessings. The content of Psalm 2 corresponds to "Es Tzemach," while the content of Psalm 10 corresponds to "v'la'Malshinim.")