This is tricky to answer, because the 17th century was a period of significant change in how European armies conducted themselves on the field, and even in the early portions of the century there was little reliance on one particular method. I am not an expert on this period, but, I feel I have read enough primary and secondary materials around the subject that I might be able to cobble together an answer mods *might* not delete.
Broadly speaking the 17th century is viewed as a period of transition in Europe from earlier Pike-and-Shot formations - often depicted as rigidly structured - to more modern thin line formations with a focus on mobility with the goal of gaining a position of advantage from which to deliver the maximum amount of firepower at the moment of maximum effectiveness.
My understanding is that in practice, the transition from pike-and-shot to thin line formations was frequently one of degrees and adopted sporadically; with the result being that depending on which military and at what point in the century, the answers will be different.
Broadly speaking there were a few general principles at play throughout the century by which cavalry threats were dealt with.
Pikes - a dense pike formation facing a charge is an effective deterrent not only to cavalry, but any formation looking to charge to close range.
Sidearms - Some musketeer formations, not all, carried sidearms for the purpose. Most musketeers carried a sword at least. However, I am aware that under Gustavus Adolphus, his musketeers typically carried a short-hafted(~6ft) polearm.
Other Cavalry - I feel like this is self-explanatory
Depth of formation/Combined Arms - Disciplined troops with formations many ranks deep allowed leading ranks to absorb, slow, and potentially halt the charge. Coupled with some combination of the other elements above, cavalry charges could be beaten back.
Massed firepower - increasingly improvements in firearms technology that appeared early in the century, and became more common throughout it(I'll get into them later), allowed infantry capable of delivering sufficiently dense volleys of firepower such that thin lines of musketeers could present sufficient massed fire to defeat cavalry.
Even MORE firepower - More, smaller and lighter artillery that could be moved by 2 or 3 man teams, and could move & support infantry lines in the field. These were apparently light enough to, if necessary, be man-handled by their teams to be quickly brought to bear against unexpected threats. Cannister shot at close range was held to be particularly effective.
The Bayonet - Muskets of the period were quite long compared to modern firearms, and there are accounts of musketeers improvising daggers into the barrels of their muskets to use as polearms, rather than try using their swords. Purpose built plug bayonets began appearing in numbers around 1660-1670, but these took time to affix, could be difficult to remove, and prevented the weapon firing. The development of the ring bayonet, which allowed the musket to continue being fired, gave soldiers a 6+ft long gun-spear.
It is past 1am - I do have particulars to offer on Spanish Tercios, line formations, and how each performed in the field. As Reddit has already refreshed two incarnations of this reply into oblivion, I'm posting this now with the intent of adding more later rather than tempting fate.