Basically the DoD allows use of their equipment, personnel, and facilities so long as the movie or TV show generally shows the military in generally good light. Normally the production company has to have the script vetted to ensure that it will pass muster. As for cost, it depends. For example giving permission to shoot on location on an Air Force base to film planes taking off and landing isn't going to cost much because that kind of activity happens all the time. But, any special requests have to be covered by the production company. Of course, the military is famous for fuzzy accounting, just like Hollywood. In some ways, I think the military cooperates because it is just like product placement, positive exposure is good and they can justify it by saying that it enhances recruiting.
I know that Michael Bay is allowed to use military equipment because the military likes how he portrays what they do on-screen. Or so he has said in interviews and the commentary track on Transformers.
Michael has it correct. This became a big deal in 1986 when the military consented to support Top Gun, and it did wonders for their recruiting. but the military did require changing Charlie from an enlisted sailor or another pilot to a civilian instructor.
The script and how it paints the military are critical to determining if the military will cooperate. Some movies have workarounds, and some directors have good relationships (again, Bay is famous for this). The production usually "rents" the platforms being used and they also pay for the fuel (a True Lies bit once talked about how much the Harriers were per hour - it was in the thousands of course). I don't believe the productions get ordinance. You got to make that work with special effects or use existing footage.
The correct spelling (and pronunciation) is "ordnance".
Sorry to be picky. I made this mistake when I first started writing design documents in my company's ordnance design group. People who have designed and/or worked with ordnance in their careers actually get picky about this.
IIRC, Crimson Tide was one instance where DoD declined to cooperate, no doubt because the conflict between captain and XO over launch of nuclear weapons was not something they liked seeing. As to how the producers worked around it, I dunno -- stock footage?
They had a boat and helicopter hang out outside one of the submarine bases and waited around until a sub came out and they just followed it until it submerged (which isn't something that would really go over real well these days).
IIRC, they actually filmed that scene in Hawaii so they lucked out on getting a boomer since they aren't home ported there.
I believe there was a disclaimer at the end of Hot Shots! about how they wish to acknowledge the complete lack of assistance by the military community or something like that. Not sure why, from my seven years in the service I'd say that portrayed it in a pretty accurate light....
Back in the mid-90s' I did movie reviews for the Navy for the weekly video magazine "Navy News This Week," and I remember putting together a review of "Down Periscope." Although the folks in the sub community LOVED the movie, the review was never aired because the movie showed authority figures in a bad light.
I remember Roland Emmerich and Dean Devlin talking about trying to get military support for Independence Day. They were all on board until they read the part about Area 51 and declined to help.