The Shulhan Aruch (Orah Haim 175:1-5) discusses the laws of reciting the blessing of “Hatov Vehametiv”. Our Sages instituted this blessing when one drinks wine during a meal and is then brought a finer wine. Because of the types of conditions that need to be met before reciting this blessing, the Poskim suggest that if one is not sufficiently learned in Torah, one should not recite it.
The first condition that needs to be met is that one needs to have first made a blessing on wine, to the exclusion of grape juice. Furthermore, one cannot recite the blessing if one finished the glass of the original wine, as it appears that the second wine is being brought out simply to refill his glass with more wine, and not because it is better quality wine. Another condition is that the finer wine must be brought from a different location, such as the kitchen, and could not have been already on the table when one drank the first wine.
There is disagreement among the rabbis as to whether “Hatov Vehametiv” is recited only when wine is drunk as part of a bread-based meal, or not. Rabbi Yitzhak Ben Oualid (Vayomer Yitzhak, vol. 1, § 1) quotes Rashi (Berachot 59b), who seems to imply that the blessing is only recited when one has wine as part of a meal which includes breads. Nevertheless, Rabbi Shalom Messas (Shemesh Umagen vol. 3, § 88) writes that the custom in Meknes was to recite “Hatov Vehametiv” during meals that did not include bread, such as an all-fruit meal on Tu B’Shvat. Indeed the custom is to be lenient and to permit the blessing in a meal that does not include bread.
Summary: The blessing of “Hatov Vehametiv” is recited when finer wine is consumed after a less fine wine. There are several conditions which need to be met before reciting this blessing.