These are two examples of photographs with black background. My question is how can I set the background like that? Was it through post-processing or on shoot?
They could have either been shot in front of a gray/black background or the background could have been transformed from a color to a shade of gray (or from a lighter shade of gray to deep black) using one of several different ways in post processing. The two most common would be to either mask out the background and replace it or use a tool that allows us to shift a particular color to a different color. We have existing questions/answers for both.
It could even be a third way: light it so that a non dark background looks totally dark as captured. It is often referred to as killing the ambient. We have questions/answers for that, too.
But in the case of your examples it's pretty clear cut that the reason the background is a shade of black/gray is because the image was processed in monochrome. Everything in both images is a tone (brightness value) of a single hue or lack of hue. The first appears to have a slightly sepia hue, the second appears to be a straight hueless grayscale monochrome.
1. Use the inverse square law.
If you have your subject well lit with one light, and the background is far away, it will receive less light because of the distance. Increase the distance and it will become darker. Of course, this is true with each light added.
This is the most important thing to understand. With this method you can even "kill" natural ambient light, using a bright light close to your subject.
2. Use a dark flag.
The idea here is simply to cast shadows on your background. It can be a grid, a barn door, or a humongous black card that prevents light from reaching the background.
3. Use a dark background
A pretty simple idea, is it not?
4. Lower the ambient light on the room
You can avoid reflections on a white painted small room adding some dark fabric as a wallpaper or curtains.
On post process
5. Color complementary colored background
You could use a color opposite to the skin tone, in this case, green or blue, and just use the red channel.
6. Mask it
Just mask the background and darken it.
Either way takes a combination of technique, equipment, and editing. The black background especially requires tight control over your lighting, so doing it with natural light can be quite difficult. In either case, grey is easier than black.
Grey: That background could actually be white, but a digital camera's white balance is going to look to render mid-tones as 50% grey. This is obviously tougher to do with film, but so long as you expose for the subject and try to meter the background a stop or two lower, you'll get results you can dodge/burn into the final photo you want. You can get grey in a black and white photo out of just about any color, including white (just don't light the background).
Black: I would use the black side of a large reflector I have, but you can also use black posterboard or just make sure you have a good amount of distance between your subject and the background. The goal is to massively underexpose the background in relation to the subject. Window light is no good because that room is going to bounce light around everywhere. You need the light coming in from a different direction from the camera angle (that Leiter photo looks to be lit from about 45 degrees above and in front of the subject) so you aren't splashing light against the background and getting exposure from it. You can make the black background even deeper either by burning it selectively or turning up the black point in processing.
Doing either is a good exercise in light control and processing. I'd recommend trying it with off-camera flashes and a flash meter. You can also get white by using a white background (anywhere from white seamless paper or a good thick bedsheet) and putting enough light on it to overexpose by several stops.
You can pretty easily do it using photo editors. I would recommend Movavi Photo Editor. It has a special tool for changing photos' backgrounds. You can change its color or use any picture to create a new background. It has lots of other cool features. They have both windows and Mac (https://www.movavi.com/mac-photo-editor/) versions.