It seems that the relatively large and easy-to-track target presented by a lightly armored or unarmored man on horseback would give a significant advantage to infantry, even during the early days of slow firearm reload and firing rates. This seems like it would hold especially true during a cavalry charge on an infantry formation, when the riders would be moving in a relatively close mass straight-on to the enemy. Were there particular tactics that prevented excessive attrition during these maneuvers? Maybe I'm significantly overestimating how vulnerable a horse is to a single gunshot wound? Any responses relating to pre-1914 warfare in any country would be welcome--I'm just trying to understand the general theory.
While you specify a pre-1914 era in your question I’d like to offer you some perspective for the British and Commonwealth cavalry of First World War.
The First World War is popularly thought of the end of cavalry – that in the face of machine-gun fire (which the generals didn’t anticipate, for some reason) cavalry were mowed down. The end of an era of “chivalric” warfare, when now men could – to use your phrasing, easily shoot and kill the “relatively large and easy-to-track target”. That’s the popular view, at least.
Reality in the First World War was a bit different and cavalry’s nadir really doesn’t come until the end of the Second World War – but I’ll leave that conflict for someone with a better grasp on the Eastern Front (in short, the Western Allies had, for the most part, dismounted and mechanized their cavalry units. The Soviets and Germans in particular had active horse cavalry operations through 1945). Cavalry units were a very valuable tool in a First World War commander’s arsenal – especially in an era of unreliable mechanized units.
So, during the First World War cavalry were able to maintain their usefulness through a few factors. The first was that cavalry units had organic machine-guns and artillery available to them. Mounted attacks were expected to be made with support from their own machine-guns and horse artillery. In the case of the British, these were Vickers Machine-Guns in 1914 and then Hotchkiss Portative in 1916. Their Horse Artillery were 13-Pounder QF artillery piece. These would be used to eliminate enemy machine-guns and artillery pieces. Furthermore, they would be used to help keep the heads down of their opponents.
Next up in the cavalry’s arsenal was dismounted fire. Cavalrymen were, for the most part, trained to be able fight both on horseback and on foot with their rifle or carbine. Cavalry tactics dictated that dismounted rifle fire would also be used to keep the enemy distracted and pinned down. So, part of the way that cavalry retained its usefulness after the advent of firearms was through the adoption and usage of firearms for suppressive fire.
The other way that cavalry was able to maintain its usefulness, in conjunction with the adoption of suppressive fire, was the speed of the horse. While, on the surface, a horse may seem slow to a modern audience, in fact, when in a danger zone, horses would be the single fastest asset on the battlefield. A full gallop is between 25 and 30 miles per hour, and while a horse would only be able to sustain such a speed for a couple of miles, that is enough to outrun the pace of artillery fire, the traversal and elevation speeds of a machine-gun, or even rifle fire (especially if it is being aimed at a longer distance). Compare this to the speed of the “fast” Whippet tank, which was able to go about 8 miles per hour maximum.
In the Battle of Beersheba, in 1917, the Australian Light Horse galloped at Ottoman trenches for approximately 1.25 miles and it took the Light Horse 2.5 minutes to cross that distance. The Ottoman soldiers had set their sights for 800 meters, but because of the speed of the charge had not been able to adjust again, and thus their fire was off. This is but one example. During the Indian Cavalry’s advance on High Wood on July 14th, 1916 – they were able to utilize their speed to escape both artillery fire and machine-gun fire as they moved up towards the wood and the German lines. At least a squadron also took the opportunity to charge a small German infantry unit. Here again speed was essential.
Another example is the Battle of Cerizy in August, 1914. There, ‘C’ Squadron of the 12th (Prince of Wales’s) Royal Lancers charged a Squadron of German Cavalry. They did so with fire support from artillery, machine-guns, and dismounted Cavalry troopers as well as surprise. I shall let the unit’s record of the attack speak:
He moved ‘C’ Squadron, who were now in dead ground, just under the ridge the enemy were lining and then advanced up the steep ridge in line of troop columns, so as to keep the horses fresh till the last moment. Just before reaching the crest line was formed, and as the squadron topped the rise “Gallop” and “Charge” were sounded in quick succession by the Regimental Trumpet-Major and taken up by the “C” Squadron trumpeter. With a ringing cheer, the Squadron charged in perfect line across the fifty yards which now only separated them from the enemy, with the Commanding Officer, his Adjutant, the Trumpet-Major, and two orderlies some twenty yards ahead of them. Though the surprise was complete, the majority of the Germans rose to their feet and fought most gallantly, though a few put up their hands, and others cowered face downwards among the roots they were laying in.
The Germans lost approximately 70 men, wounded and dead – while the 12th Lancers suffered 4 killed and 6 wounded.
In short, Cavalry during the First World War were able to maintain their relevancy through the adoption of firearms, speed, and surprise. Surprise would often be made through the usage of dismounted fire to create an opportunity for a mounted attack to occur. It’s as the British cavalry manual used during the war states: “The rifle endows cavalry with great independence in war, […] for fire action can create favourable opportunities for shock action, and a well executed combination of the two methods will often present the greatest chances of success”.
Edit: Figured an addendum of some further reading might be useful!
If this answer seems limited in scope and potentially incomplete, that's for a couple of reasons. Firstly, 'the advent of firearms' was not particularly long after the inception of gunpowder itself: the original fire-lance, a device originally used as a primitive flamethrower but eventually modified to propel pellets and fragments, first made a definitive appearance in a battle between the Chinese state of Song and the Jurchen state of Jin in 1132, and there are older, but brief textual references to a 'fire lance' in Song inventories in 1000 and 1044. Recognisable conventional guns, albeit often of very small calibre, have traditionally been dated c. 1280 in China (suggested by archaeological finds) and c. 1327 in Europe (when they appear in both textual and illustrated accounts), but some less certain archaeology suggests cannons in use by the Tangut state of Western Xia as early as the 1220s. That means that 'after the advent of firearms' could mean at least 700, and potentially up to 1000 years of history! Secondly, there's a huge amount of world to cover, even if we confine ourselves to the Eurasian continent. Thirdly, some of us flairs have been conspiring to address different aspects, as you have already seen with the WWI answer that's gone up before my own.
As such, my answer will focus specifically on Early Modern China, Inner Asia, and Central Asia. Part of this, for obvious reasons, is that polities maintaining cavalry-centric armies held out in Central and Inner Asia the longest, the strategic and operational reasons for which I discuss, in brief, here. What I didn't discuss was the tactical aspect of how cavalry combat became less viable. Now, you've asked how cavalry retained its usefulness, which entails a discussion of cavalry tactics which I will get into a bit, but it's also worth considering why cavalry retained its usefulness: what, on the other side of the coin, were the limitations of gunpowder weaponry? These elements go hand-in-hand. As such, I'll be going slightly in the opposite direction: what were gunpowder's limitations, and how could cavalry exploit them?
Firstly, most ranged weapons require a line of sight to the target. If operating in uneven terrain, cavalry can, so to speak, 'get the drop' on infantry and artillery by exploiting that terrain and minimising the time they spend under fire. At the Battle of Sarhū in Manchuria in 1619, Jurchen cavalry under Nurgaci defeated a gunpowder-heavy Ming force by hiding in forests and behind hills; in the latter case, they could quickly ride up onto those hills to get in bowshot of the Chinese and Koreans, having avoided being in range of their firearms beforehand. Obviously ambushes require a degree of luck and a relative familiarity with the terrain compared to one's opponents, and this was not the principal factor.
Secondly, earlier guns had a comparatively limited rate and density of fire. The earlier you go, the harder a cannon is to load, generally speaking: castings were rougher, there was less precedent for using prepackaged charges, and the exact procedures were still being worked out. But the issue was even bigger for matchlocks, which require a more complicated set of operations to reload and fire than the later flintlocks, which already, under ideal circumstances, took at least 15 seconds to load for a well-trained infantryman. Besides that, they are also not only impractical, but even potentially dangerous to use in too dense a formation, as you need room to swing the gun to inspect the slow-match, which also risks coming into contact with someone else's clothing or ammunition if standing too close. While small skirmishes very much favoured the musket, in larger engagements, the advantage of foot musketry over horseback archery diminished substantially. Russian musketeers often struggled against Central Asian horse archers well into the eighteenth century, with the speed of the horse compensating for the range and accuracy of the matchlock, and with its rate of fire overwhelmed by the nomads' bows. Going back to Sarhū, the Jurchens' success was not just about exchanging fire while avoiding being outranged: the goal was to charge, and they did so successfully. Even on relatively open stretches, Ming troops only got one or two shots off before the Jurchen cavalry came into contact and engaged in hand-to-hand combat with a decisive advantage. However, the introduction of flintlock muskets, which could be used in denser formations and reloaded much faster, pushed musket-armed infantry over the decisive threshold, so to speak. The elite musketeers of the Persian warlord Nadir Shah devastated Mughal cavalry at the Battle of Karnal near Delhi in 1739, and swept away Uzbek horsemen during campaigns in Central Asia between 1737 and 1740. It was this defeat against the Persians that led to many of the Central Asian states adopting a military model built around a core of elite musketeers and artillerists, over the prior method of simply rallying tribal cavalry as needed, but even then the cavalry were retained for numbers if nothing else.
Thirdly, guns and horses are not mutually exclusive. Cavalry could operate in conjunction with other arms for greater effectiveness, or gunpowder could be integrated into cavalry forces. You've seen examples of the former in WWI from /u/IlluminatiRex, but to offer up an Early Modern Asian case, at the Siege of Dalinghe in autumn 1631, the Jurchens under Hong Taiji arrived with a massively expanded artillery train which quickly wore down the Ming defensive works and forced them to sally out, at which point the Banner cavalry rode out to cut them down. Artillery could be integrated into the cavalry formations themselves: European armies set up contingents of horse artillery, expected to keep pace with the cavalry and provide fire support where needed; Asian armies seem not to have adopted the limber until acquiring European guns wholesale, but many armies near Central Asia did adopt camel-mounted artillery, often the lighter zamburak, though the Qing also loaded camels with heavier-calibre guns. And indeed cavalry could use firearms: the Qing, for instance produced matchlock carbines that were known to be used from horseback, and you can see an illustrated example from this manual for Chinese soldiers. Of course, a somewhat more common technique was to ride in on horseback and dismount to fire ('mounted infantry', so to speak) – Nadir Shah's elite jazayerchi corps was such a formation; so too was the Qing Huoqiying ('Firearms Brigade').
Fourthly, gunpowder is expensive and especially so for nomadic societies, while horses are cheap on the steppe but ramp up quickly in cost the further you get. In this answer on European manure control I note the immense resources expended on obtaining saltpetre for making gunpowder: the limitation was often not so much financial capital as it was social and political capital, given the sheer scale of the private property that would be encroached upon to dig up nitrate-rich soils in places where otherwise naturally-occurring sources were rare. And that's with the developed apparatus of the Early Modern state, building on the full scale of primitive chemical production. What chance did steppe nomads have of producing that much powder? In Europe, it wasn't until after around 1700, following decades of bureaucratic expansion and military enterprising, that it became viable to equip an army's entire infantry with muskets (though another limiting factor had been the lack of bayonets that allowed muskets to be used as basic polearms); the Qing only maintained a few small corps of fully-firearm-equipped troops before reforms following the defeat to Japan in 1895, and even then there were plenty of archaic-equipped militia and provincial units down to the revolution in 1911. By contrast, to raise horses you only need grassland, which nomadic polities had a lot of, but which sedentary ones would likely turn over to farming or for grazing edible livestock. But, sedentary polities might opt to maintain large cavalry forces if in proximity to horse-based societies, supplementing smaller gunpowder cores – this is what the Safavids of Iran did, and which Nadir Shah further refined. For those further afield, however, the difference in cost between gunpowder and cavalry was not so appreciable.
So, to convert all that into a concise summation, cavalry retained its usefulness into the Early Modern period because: 1) earlier gunpowder weapons did not offer enough of a force multiplier to infantry to give them complete or even particularly effective protection; 2) In many cases, the cost of raising cavalry was much lower than attempting to supply gunpowder to infantry and artillery; and 3) cavalry could itself make use of gunpowder.
Sources and Further Reading
Nicola di Cosmo, 'Did Guns Matter? Firearms in the Qing Formation' (2004)
Peter C. Perdue, China Marches West: The Qing Conquest of Central Eurasia (2005)
Michael Axworthy, The Sword of Persia: Nader Shah, from Tribal Warrior to Conquering Tyrant (2006)
Scott C. Levi, The Bukharan Crisis: A Connected History of 18th Century Central Asia (2020)
Scott C. Levi, 'Asia in the Gunpowder Revolution' (2018)
Tonio Andrade, The Gunpowder Age: China, Military Innovation, and the Rise of the West in World History (2016)
IlluminatiRex's excellent answer has related how cavalry could still be effective into the 20th century, but now step back a bit. There are guns and guns, and not all are the same. The "advent of firearms" covers a lot of history, and for most of that history infantry firearms meant smoothbore muzzle loading muskets. These were single shot weapons with a complex reloading drill, trained troops could be expected to be able to get off about 3 shots a minute. In 20 seconds, a horse galloping at 25 mph on flat ground will cover 244 yards. Which is right at the outer edge of the effective range of a musket, if not outright beyond it. This means that when cavalry charged infantry, the infantry were going to get one shot, then the cavalry would be on them.
This why you see so many crusty NCOs in old movies calling "don't fire until you see the whites of their eyes!" In practice many of these movies aren't really set in the period when this was relevant, but in the days of muskets, it was horribly real. One shot was all you were going to get. It was a matter of life and death to save it until the enemy was close enough you were pretty sure to hit something.
In practice, you simply cannot expect to stop a cavalry charge with a single volley. Many shots will miss (smoothbore muskets were pathetically inaccurate), some will be wasted due to multiple shots hitting the same target, and a significant number will misfire (in the rain, MOST will misfire). The infantry of the era had to rely on melee weapons to protect themselves. In the early days of firearms, muskets were fielded alongside pikes, with the pikes protecting both from cavalry. Bayonets were eventually developed that allowed a musketeer to use his musket as an ersatz pike (shorter and clumsier, but used carefully in combination with firepower, often good enough). Guns were useful, but they did not rule the battlefield the way they do now, with modern rifles being much longer ranged and way faster firing.
Also keep in mind that direct action on the battlefield is only part of the cavalry's job, and often the smaller part. Horse cavalry was used for reconnaissance, keeping an eye on the enemy but remaining out of weapons range, a role it was much better suited for then infantry. Additionally, cavalry was used to drive off the enemy reconnaissance groups. Cavalry was also used for raiding behind enemy lines, attacking undefended or lightly defended targets by surprise, then withdrawing before reinforcements could arrive. In those roles cavalry remained king for a long time even after improvements in firearms made infantry and artillery the main arbiters of pitched battles.