In my very limited experience (I was in a low-budget documentary once, AMA!), the filmmakers come to you. They tend to scour the media they have access to - that is, popular history publications and other documentaries, rather than academic work - for people who might be able to speak on the subject they are doing an episode on. Then they reach out to those people and see if they are willing to be interviewed for free.
The quality of commentators depends on a lot of different factors. One thing, of course, is the degree to which the director and his staff care about academic credibility. They are ultimately in the business of making entertainment, and the role of talking heads is often to create variation in the narrative voice and an illusion of accountability rather than to supply cutting-edge insight. A related factor is the extent to which the documentary makers have their minds made up about the script. Some are very willing to adjust their story based on what the experts tell them, whereas others pretty much just want to have their story interspersed with soundbites from experts. Depending on where they stand, directors may care a lot or not at all about the credentials of their talking heads. In the former case, they might actually try to reach out to top-of-the-line academics. In the latter case, anyone willing to repeat a predetermined point while looking vaguely like they know what they're talking about will do.
Other factors are with the invited consultants themselves. Bertrand Russell's old chestnut about fools being full of confidence and wise men full of doubt is very relevant here. A lot of established academics are hesitant to take part in documentaries and the like, perhaps partly out of snobbery, but also out of doubts about their own level of expertise or ability to present it well, or worry that they might be misrepresented. Disillusionment due to bad experiences in the past (being misquoted, quoted only on inane things, or having one's part cut altogether) often plays a role. On the other hand, people with little academic credibility might be more than happy to speak about any number of subjects they've done some work on. As I said, documentary makers will look at other documentaries for examples, meaning that those who appear often will appear more often, regardless of how good they really are.
In my own case, I was contacted through my LinkedIn profile (unbelievably, this actually happens) because I had published a piece relevant to the planned documentary in Ancient Warfare Magazine. I talked to the documentary makers at length over the phone to establish how serious they were and how willing they were to let me make my own point in the interview. They were happy to have an academic expert enthusiastic to contribute, I was very pleased with their open-minded and constructive attitude, and the eventual interview was a really positive experience. When I saw the final product, I realised that I was actually one of the most junior people they interviewed, which shows their commitment to getting some real experts to weigh in - though all of the others had famously been in other documentaries, suggesting the documentary makers' method.