For the state: Why would you want an army that wasn't based on meritocracy? For the commander: why would you want to pay for a position, rather than be paid for it?
It has to do with how armies developed. In medieval Europe and parts of Asia like Japan, feudal arrangements developed where a noble or warrior class was given a certain fief of land (or, later on, a money fief tied to a specific town or city) in exchange for raising a certain number of troops when required. Military service for the nobility (or at least the warrior class of the nobility) was thus fairly compulsory, and became part of the identity of that class.
The transition to standing armies and semi-professional (i.e. conscript-driven) and professional armies in the early modern period led to a decline in the traditional military need for and importance of the nobility, which was in part resisted as military occupations were still a part of noble identity, as an acceptable or honorable career. The commission purchase system could be seen as a descendant of the feudal "fine" or "relief" system (where the heir to a title would have to pay a tax to the monarch to inherit the title), but in the British military in particular it appears to have been largely a classist custom, designed primarily to keep the upper ranks more-or-less exclusively the preserve of "gentlemen."
And you can see that there was a struggle between this tradition and the practicality of having officers promoted by merit, which is why in the British system it didn't apply to the artillery, engineers, or navy. The state (or monarch, during the early part of the system) still had some control over officers that couldn't perform (they could be "cashiered" out), and the people running the country were often of the same class as the officer corps, so the same biases were at work - plus, it provided the officer selling the commission a suitable "retirement" sum.
For more details on the subject, you might want to read The Purchase System in the British Army, 1660-1871.
The question comes up from time to time. While any new answers are welcome here is a previous discussion of the issue https://www.reddit.com/r/AskHistorians/comments/1hnrbx/what_were_the_effects_of_the_purchasable/
The biggest risk for a government is generally it's own army.
A purchase system tries to ensure you dont get some landless adventurer in a position where he can use his army to depose his government.