Doing advanced, completely unprofitable basic research, for one thing. These are the sorts of projects that spawned GPS, the Internet, and IC manufacturing. Wasn't super profitable at the time of conception, but the military doesn't care. They need capability, irrespective of price. All of these technologies are now keystones of numerous gigantic American companies with billion dollar plus valuations.
Building up US based scientific and manufacturing know-how, for another. Plenty of people take the skills they learn at Raytheon or an FFRDC and use them to start profitable small businesses, which in turn employ more people and generate more tax revenue.
Not that I expect many people reading this to care about that. "Military spending bad" is too easy and convenient a slogan.
It doesn't solve every problem - there are a shitload of people in America receiving inadequate education, nutrition, and healthcare, for example. But it certainly helps keep a lot of Americans employed and paying their mortgages, which is a non-trivial benefit.