Considering that a black hole's gravity prevents light from escaping, how can a black hole emit X-rays? Visible light and X-rays are both electromagnetic radiation, shouldn't the black hole's gravity prevent X-rays from escaping?
The X-rays come from hot gas orbiting around the black hole in an accretion disk. As the gas orbits, magnetic stresses cause it to lose energy and angular momentum, thus spiralling slowly in towards the black hole. The orbital energy is transformed into thermal energy, heating up the gas to millions of degrees, so it then emits blackbody radiation in the X-ray band.
Once the gas gets closer than a few times the horizon radius, it plunges into the black hole, so while some X-rays can still escape just before the horizon, most are emitted a fair bit outside.
My understanding is that any radiation emitted "from a black hole" is actually originating from or just outside the event horizon of the black hole. This is the region beyond which the escape velocity is at or above the speed of light (electromagnetic radiation). Outside the event horizon, escape is still possible.
There is another kind of radiation, however, called Hawking radiation which theoretically can escape from a black hole. This involves some quantum effects.
The x-rays are not coming from the blackhole itself, they are created by the incredible forces that are enacted on objects that start going past the event horizon. Think of it this way:
If you went feet first into a blackhole past the event horizon, your feet will get pulled much faster than your arms or head. This will cause your stomach to be ripped apart in such a strong and sudden way that it will start emitting x-rays, and since your stomach is still outside the event horizon, those rays are able to escape.
Black holes are formed from collapsed supermassive stars. If a black hole ends up completely isolated then it will be very black. However, many stars are binaries. Moreover, within dense globular clusters stars can often pass very close to other stars. Just as with stars, when a black hole is near enough to another star it can collect matter from it. Black holes are incredibly compact objects with radii of only about 3km per solar mass. As matter falls onto such a compact object it will accelerate to high speeds and also be constrained to travel through a small volume of space, causing it to have a high chance of colliding with other matter falling into the black hole. This process creates an orbiting accretion disc of matter spiraling into the black hole. This matter is travelling so quickly and compressed so much that it is raised to enormous temperatures, so high that it emits a significant amount of energy as high energy EM radiation such as x-rays. Indeed, this process is by far the most efficient known process of converting matter to energy (an order of magnitude more efficient than nuclear fusion).
So far a few people have answered (correctly) that X-rays are generated by BH-star binary systems from a thermalized accretion disk. This process produces soft (low energy) X-ray radiation. In some systems, the high energy emission is much harder then that of a thermal disk and dominated by synchrotron X-rays emitted from shocked gas in the jet.