There are four distinct themes in birchas hamazon. Each theme requires its own beracha. We first thank Hashem for food by reciting the beracha of "hazan es hakol". This beracha is followed by the beracha "al ha'aretz" - thanking Hashem for Eretz Yisroel. We conclude birchas hamazon on a Torah level with the beracha of "bonei Yerushalayim". Chazal added the fourth beracha of hatov v'hamaitiv focusing on the goodness of Hashem.
The first two berachos are easily understood as integral to the mitzvah. We mention food and the land where the food comes from. However, the third beracha is questionable. Why is it necessary to mention Yeruhslayim in our birchas hamazon? What is the connection between what we ate and Yerushalayim? The fourth beracha added by chazal seems to be superfluous. After we have blessed Hashem for food, Eretz Yisroel, and Yerushalayim, what do we add by an all encompassing statement of hatov v'hamaitiv?
Mentioning Yerushalayim in birchas hamazon puts our entire meal in a different perspective. The Torah requires of us to separate ma'aser sheini - a tenth of all our produce. This ma'aser is eaten in Yerushalayim in a state of purity. What appears to be a simple act of eating is described (Devarim 14:23) as a method of attaining yiras shomayim. Yerushalayim has the unique ability of being able to transform the physical world into a spiritual one. Meat, wine, and fruit are no longer physical pleasures but rather korbanos and ma'aser sheini, vehicles used in avodas Hashem. When we complete a meal we must mention Yerushalayim thereby reinforcing that we eat not for physical pleasure, but to enable us to serve Hashem. Eating a simple sandwich is transformed into a spiritual experience by recounting the special quality of Yerushalayim.
Chazal instituted the fourth beracha of birchas hamazon following the tragic events of Beitar. After the churban the city of Beitar was destroyed and multitudes of Jews were killed. Despite this horrible tragedy, Chazal saw a glimmer of good during this event. Although initially the Jewish bodies from Beitar were not allowed to be buried, eventually permission was granted by the Romans to do so. Miraculously, the bodies did not decompose in the interim. Chazal took note of this double blessing and instituted a dual phrase of thanks. The phrase "hatov" thanks Hashem for enabling the burial of the dead, and "v'hamaitiv" thanks Hashem for preserving the bodies until burial.
Why was this beracha incorporated into birchas hamazon? Hatov v'hamativi teaches us to look for the good even in tragedy. This lesson is critical in order for us to perform the mitzvah of birchas hamazon properly. It is easy to bless Hashem after an entire meal when we are blessed with having all of our physical needs fulfilled. But, how do we react when we are lacking a complete meal and our physical needs are apparently not met? One would think that there is no need to thank Hashem for the little we have been given if we lack so much. The beracha of hatov v'hamaitiv instructs us otherwise. Even at a time of tragedy such as the destruction of Beitar we must search for the goodness of Hashem. Even if we perceive ourselves as not blessed with physical abundance we must appreciate whatever blessing Hashem has bestowed upon us.
When we recite birchas hamazon we thank Hashem for the physical gifts of food and Eretz Yisroel. We then focus on our spiritual existence through recounting Yerushalayim. Once we understand that our physical gifts are only granted to us to enable us to grow spiritually, we realize that whatever Hashem has given us is truly good and sufficient. Perhaps this is why Chazal derived the requirement to mention Yerushalayim from the phrase "ha'aretz hatova" as the word "tov" refers in other places to Yerushalayim. If we focus only on the physical, we will complain if our physical gifts are limited. Yerushalayim is the ultimate "tov". It teaches us that even the limited physical blessing is only to enable spiritual growth. Thus it teaches us that everything is "tov". Only after mentioning Yerushalayim can we conclude our birchas hamazon by saying "baruch hatov v'hamaitiv".