The matchlock was a pretty simple device- the earlier forms had very few moving parts, with a pan cover to be pulled aside and a finger lever connected to the cock holding the slow match. At the start of the 15th c. the wheel lock appeared, which may or may not have been based on an existed fire-starting device. Unlike the matchlock, these were concealable- and because of this there was briefly a ban on wheel lock pistols by Charles V. Quite soon after the advent of the wheel lock, the snaphance appears. This was basically a flintlock, but simpler: in the early forms a bit awkward, as the pan cover had to be pulled aside before firing.
Circa 1630 the most common of these, for the common soldier, was the matchlock- it was the cheapest to make, and in the pre-industrial world, manufacturing enough guns for an army was itself enough of a challenge. The first flintlocks actually we can say were invented by two men, French gunsmiths -brothers- Jean & Marin le Bourgeoys, c. 1610. Marin had his own workshop in the Louvre where he produced guns for his main customer , Louis XIII. The loading and firing drill for a flintlock was simpler and faster than for a matchlock, and did not require the soldier to be handling loose gunpowder and burning match at the same time. With that advantage flintlocks made their real appearance as martial arms mostly in the Thirty Years War, and they would become pretty dominant after 1660. But there would still be matchlocks about until the end of the 17th c. Obsolete, yes: but still useful for something, at a time when things were generally quite expensive and so, generally, used as long as possible. As far as advantages: flintlocks needed flints, matchlocks needed slow match. Slow match was perhaps easier to make, ship, and supply. But it also had to be kept dry, whereas flints did not.
The 17th century, even into the early 18th century is probably the best time to consider the transition from matchlock to flintlock, at least in Europe. This generalisation comes with its own problems, not least that what we term 'flintlocks' were actually a collection of different types and designs of 'locks' that were being developed, improved, tweaked, and adapted across the period and in different nations. These include (but are not limited to) English lock, the Doglock, the French lock, the Snaphance, the snaplock, and the miquelet which were all different variations, although when we think of when we think of as a flintlock mechanism was probably (as a previous answer gave) the work of le Bourgeoys at the beginning of the 1600s. However, examples of flintlock mechanisms, such as the snaplock, predate even this and 'proto-flintlocks' maybe have been around for almost 100 years previously with wealthy members of the Elizabethan nobility enjoying 'snaphaunce' hunting guns.
This long cross over period was due to several factors, not least cost as earlier flintlocks were terribly expensive compared to the matchlock, especially in bulk orders. A flintlock in 1643 would cost £1 2s, whereas a matchlock only 12s 6d (that is almost twice as much for a flintlock 22 shillings vs 12 and a half shillings). A matchlock musket was easier, cheaper, and quicker to make (and repair) than a flintlock, and you needed less skill to do so. At the outbreak of the British Civil Wars in 1638, many non-gunsmiths could provide matchlock muskets, or at least parts for them, whereas the skill to make (one of the many non-standard) forms of flintlocks was much rarer. Add to this that the contemporary sources tend not to specify too much in regards to what type of flintlock action was being used, often using terms which really meant "any type of flintlock mechanism" which was 'firelock'.
Throughout the wars of the seventeenth century, the limitation on production of flintlocks generally meant they were focused to specific 'specialist' units, for whom the flintlock allowed a better performance of their tasks. Key among these were cavalry and dragoon weapons, as matchlocks on horseback were next to impossible (keeping a lit length of slowmatch match was unsustainable on horseback). Similarly 'firelock' weapons were issued to the guards of the artillery train of an army. It turned out having the men guarding the army's powder supply patrolling around it with lit slowmatch invited...accidents.
If possible small numbers of 'firelocks' would be issued to sentries on an informal basis, or those on raiding or patrol duties such as 'beating up the quarters' as they were seen as more reliable for those purposes. Elite companies of 'firelocks' were also raised within prestigious regiments or by particular commanders. Prince Rupert maintained at least one company of firelocks within his own infantry regiment, as too did the Parliamentarian Sir William Brereton who secured the services of a whole company of firelocks (veterans of the fighting in Ireland) who turned coat and joined his forces in 1644.
To answer the last part of your question, if commanders could get them, they certainly used flintlocks, despite their extra production costs both in money and time as well as maintenance. However, these drawbacks also meant that there were rarely enough to go around and only as the production of flintlocks became easier, cheaper, and therefore more commonplace, did production levels rise, did it eventually supersede the matchlock. However, this took rather a long time to occur and so the basic, simple, reliable, and cheap matchlock remained in service for far longer than one might imagine given the flintlock's existence.
UK National Archives, State Papers, 28/11, i, f.9, receipts to the New Model Army 1645
Reid, D., English Civil War Firearms, (Partizan Press, 1989)
Edwards, P., Dealing in Death: The Arms Trade and the British Civil Wars, 1638-1652 (Sutton, 2000), p. 12