Is it possible to apply for a PhD through a government or a military research laboratory?
If so what would be the process? Is the qualifying exam required? How is that scheduled? How about course work?
In the US, you cannot have your PhD degree awarded by a government or military laboratory, and similarly industrial labs generally cannot award degrees, either.
However, having worked at several government labs, I can confirm that it is possible to be a grad student and do your thesis research at a government lab. Usually this is done under a co-advising arrangement within an existing collaboration (Prof. X at university A works with Dr. Y at lab B), or under a cross-affiliation (Prof. X has a joint appointment at both university A and lab B).
More specific details differ too much depending on the university and department's regulations, as well as those of the lab in which you're interested in working, so you'd need to ask them specifically. However, it is also worth noting that many graduate departments still have a "residency requirement" on the books—which states that you must spend some number of semesters actually registered as a student at the university before receiving a PhD.
I am in the UK so in other countries things may be different.
Some government research institutes fund PhDs, where you are based at the institute for your research. However the PhD itself must be awarded by an academic institution (i.e. a university). You will generally have a supervisor at this university and its name will be on the certificate.
The specifics are going to vary for place to place including: what, if any, taught courses you must do, how often you must visit your university etc. In my experience these factors are often a compromise between the two institutions, particularly the university often has various requirements for the PhD while the research institute just wants you to do research for them.
As for applying, you should expect the same/similar requirements to a standard PhD. when looking for positions the host university or the industrial lab are the best places to start. Although it is not always clear whether the position is just funded by the lab, but based at the university, or actually based at the lab.
A related option, not sure how location dependent it is, is doing an EngD. These tend to be more industry focused, often in a company.
There are very few exceptions to this, and those exceptions that exist are usually simply collaborations between an institution and a PhD-granting academic institution. For example, the Danish Council for Independent Research funds PhDs that are hosted at research institutions that do not normally grant PhDs. However, you have to maintain and actually earn the degree from a university, even if you spend the entire time working at the non-academic institution.
Similarly, some institutions that are not universities grant PhDs. For example, the RAND Corporation runs a graduate program in public policy analysis, even though it is not a university and grants no degrees other than that single PhD.
In general, however, you need to be at a research university to earn a PhD.