We investigate the role of networks of military alliances in preventing or encouraging wars between groups of countries. A country is vulnerable to attack if some allied group of countries can defeat the defending country and its (remaining) allies based on their collective military strengths. We show that there do not exist any networks which contain no vulnerable countries and that are stable against the pairwise addition of a new alliance as well as against the unilateral deletion of any existing alliance. We then show that economic benefits from international trade provide incentives to form alliances in ways that restore stability and prevent wars, both by increasing the density of alliances so that countries are less vulnerable and by removing the incentives of countries to attack their allies. In closing, we examine historical data on interstate wars and trade, noting that a dramatic (more than ten-fold) drop in the rate of interstate wars since 1950 is paralleled by the advent of nuclear weapons and an unprecedented growth in trade over the same period, matched with a similar densification and stabilization of alliances, consistent with the model.