X-rays are the result of bombarding a metal surface with high speed electrons. This speed is achieved by subjecting the electrons to a large potential difference. This p.d. causes the electrons to accelerate to a higher speed than usual.
Electromagnetic radiation (Bremsstrahlung radiation) is created, with the wavelength of the rays varying inversely with the acceleration of the particles.
The X-ray tube
This is a device used to produce X-rays. The cathode is heated and electrons are ejected as a result (thermionic emission). By applying a very large potential difference (in the range of $20-100kV$), we cause the electrons to accelerate towards the anode. As these electrons strike the anode, a large deceleration occurs.
Image Credits: Radiology Cafe

The kinetic energy that belonged to the electrons is lost as heat energy with a small fraction being converted into radiation. The wavelengths of this radiation corresponds with the X-ray region of the EM spectrum.
The X-rays leave the tube via the X-ray window.