Gunpowder, also known as "Black powder", was used in firearms as early as the 1100s in China and the 1300s in many other parts of the world including Europe. By the early 1500s self-igniting firearms like the wheellock pistol and rifle began to appear. While many D&D campaigns replace gunpowder and technology with magic, some referees may choose to include firearms in their game world - either as a regular item, or as a "magical" device from some other world.
Even the earliest hand-held firearms, or "hand canons", had superior armor piercing capabilities. A longbow was somewhat effective against chain-mail, and a crossbow slightly more so, but a hand cannon (and later firearms) could pierce plate armour. While less accurate and slower than a longbow, a hand cannon didn't require the extensive training and practice required of a proficient archer.
As firearm technology progressed their reliability, range, accuracy and speed of reloading all continued to improve. In the 1800s weapons that could fire multiple shots before reloading appeared, and in the late 1800s and early 1900s fully automatic firing weapons were developed.
The rules for using firearms in your D&D game depend to some extent on how much detail you want to use, as well as the era of firearm you want to include. Any of the following optional rules can be used:
Hit-Point / Stamina Loss
Pistol: d6*
Rifle: d10*
* Any time you roll maximum damage you can roll again for additional damage.
Example: An attacker with a pistol rolls a d6 and gets a '6'. They roll the d6 again and get a '4'. The total for the attack is 10 points.
Armour Penetration
Armour is less effective against firearms. Reduce the AC bonus from armour by 4 points. Note: this does not effect AC bonuses from dexterity or magic.
If the optional rules for Shields are used, a shield roll is not made against attacks from firearms.
Example: A character with leather armour (AC 7) and a +1 bonus to AC from high dexterity (AC 6) is attacked by a character with a rifle. Their AC is treated as 8 for the attack (No AC bonus from armour, but +1 bonus from dexterity)
Knock Down
Firearm attacks carry a lot of kinetic energy. What constitutes a "hit" rather than using up stamina and luck varies from game to game. A suggestion is that in a system that clearly defines when a character is hit that a firearm attack also include some sort of "knock down" effect.
In D&D 4e this could be represented by a character who is hit by a firearm attack and bloodied being moved back 1 square away from the direction of the attack and knocked prone.
Reloading
Most earlier weapons could fire only a single shot before needing to be reloaded. Characters might carry a pair (or "brace") of pistols, or have a retainer who reloads rifles for them. Generally it should take a similar amount of time to reload a firearm as it does a crossbow (eg. spend 1 round reloading between attacks).
In the same way that gunpowder and firearms changed the way battles were fought in the real world, I imagine their use in a D&D campaign could also affect both the armour and equipment characters use as well as the tactics they employ. While not suitable for every game, they can make an interesting addition to a non-standard fantasy game world.