Balancing guns -- or really any weapon in a fantasy setting is largely a case of expressing the weapon's values. As we saw previously action economy has a lot to do with whether a weapon is good or bad. A lot of this has to do with the action cost of the medieval weapons in question. If a longbow shot only costs 1 action, then a gun really can't do better which is actually pretty good for balance.
Once you discuss action economy we can also discuss damage or special qualities. You can dial those up or down to make guns more or less attractive.
But that's all gameplay. How viable are guns in a fantasy setting, really? The answer is pretty viable. Fantasy settings are actually more likely to develop guns than real life. This is both from a societal development standpoint and a mechanical science standpoint.
Black powder is one of the easiest compounds in the world to make. It's all made from stuff that is easily obtainable in a fantasy setting. The hardest thing to discover would be sulfur, but if we're talking about a setting that has steel weapons (a pretty safe bet) and gold piece currency, sulfur is almost certainly going to be mined too. It's only a matter of time before people figure out uses for it. Black powder and steelworking is all that's required to make a gun. If a sword can be forged and a crossbow can be built, a gun's parts are considerably simple to make. We can go quite a ways (up to flintlock guns) without anything except steel, wood, flint, black powder, and lead (for the bullet). If lead is rare, other material like bismuth could be used (though lead is more common in the real world).
All of those components can be created -- possibly even mass-produced -- by various types of magic in most fantasy settings. Most fantasy settings put restrictions that keep players from making precious metals or magic components, but wood or steel or lead? Unheard of. Black powder, again, is a simple compound that was discovered before Jesus was born in our world. If steel forging has been invented and black powder hasn't, sulfur doesn't exist in your world or something crazy like that. Either way, conjuring gun parts is a possible, even likely way for guns to work.
Alchemy is another big deal in the fantasy world. Magic in general is pretty crazy but alchemy takes it to a much smaller, more easily managed level. How likely is it in a world where you can make magic flashbangs or magic glue that you can't make magic gun propellant? I'm sure that in any fantasy world, alchemy makes the discovery of new chemicals even better than black powder more likely. Their invention of smokeless powder might be nothing like our nitrocellulose but it might be even more effective. In the real world it only takes a few grains of smokeless powder to fire a pistol bullet. Even if it cost some magical materials, it would probably be not as costly as a big flask of alchemist's fire that makes a big explosion just because the resulting magical propellant would likely be pretty efficient. Alchemy could also allow skipping steps that took our science a long time, like the discovery of percussion caps. It took our science centuries of using black powder guns as a regular infantry tool before we discovered fulminate of mercury for our caplock guns. Alchemy gives a fast track since it's not as limited as chemistry.
Alchemy is in a really good spot too due to the position magic holds in a fantasy setting. Quite frankly, magic is OP as hell. Warfare in a fantasy setting should be utterly dominated by magicians. They can heal, raise the dead, rain flaming death, teleport, mind control, turn invisible, shapeshift etc. etc. If alchemy is a normal science that anyone can learn, it means that these scientists are likely working to deal with ways to give common people some kind of equalization in a world utterly dominated by spellcasters. This is even more so if people are born with magic or at least some kind of magical talent. This is why crossbows and guns were invented in the first place; it took years and years of training to learn the sword, the bow, and fighting on horseback. It takes a week to train a crossbowman or musketeer. Huge difference! Now imagine the same situation, but instead it's years and years to learn magic or only people born with magic can use it. How do people compete? They develop sciences like alchemy to even the playing field.
This isn't due to envy or anything like that. In a big military you can't rely on the 5+ year veterans of swordplay or wizardry. Your have a finite amount of time to train your soldiers and sailors, typically a month or two. Spending more time on that costs more! You want to get your soldiers to be the most combat-effective in the least time. Manufacturing tools like alchemy flashbangs or firearms is a way of reducing that gap. There might even be alchemy breaching charges or anti-fortification rocket launchers -- anything to make your army less dependent on the 1% of your population that have magical talent. Wizards might even help with research in order to lessen the amount of burden on them.
To a lesser degree this is also true of society as a whole. We develop sciences to allow our less-skilled people to do more skilled things. Think about computers in the 1980s, then in the 1990s, and finally today. In the 1980s you needed a ton of experience to handle a computer at all. You needed to memorize commands and had to be resourceful to get documentation for things you didn't know. In the 90s, things were easier, especially after Windows and Mac computers were released, but you still needed a CCNA to set up a router until late in the decade. Today, an untrained person can bridge his laptop with his cellphone in order to get internet. Win8 even defrags your hard drive for you now. Touchscreens and GUI interfaces have made computers tons easier. IT professionals have it a lot harder because the end user has it easier.
Still not convinced? Consider white rice. It's a pretty simple thing. Without anything more advanced than a pot, water and some firewood, you can cook it. It takes some skill to start the fire without matches or flint, though. You have to know what you're doing on that front. Fortunately, we have firemaking tools to help with that. Then we have to stoke the fire and keep it stoked. Fortunately, we can just dump the fire altogether because we invented the stove. Then you have to watch your water level and time when you want to uncover the rice. Undercooking it is pretty bad, overcooking it is slightly better but still bad. We invented glass lids to help with that, but in the end we invented a rice cooker so you don't even have to think about it. The machine takes literally 100% of the skills out of cooking rice, after multiple other inventions made it easier and easier and easier. That's science!
In a fantasy world, science is going to advance in a fantastical way that tries to make magic obsolete. Because magic exists and alchemy exists and real science exists, there's going to be constant pressure to create magical effects by other means, whether that's mechanical or alchemical or through some other science. Firearms are just one possibility of scientific evolution but in terms of military advancement they're far from the least likely. Because they existed without any sort of magic to speed their development, it's pretty likely that in a magical setting, they're pretty likely to be a really big deal.
This is assuming that fantasy society is half as violent as our society is. If it's anything like most fantasy settings, it's probably way more violent.