Racecraft is a valuable skill to have. In essence, racecraft is your ability to change your driving to better respond to the other cars around you - often, this means taking racing lines (the path through a turn) other than the fastest one in order to potentially give yourself an advantage against the driver you're battling, but racecraft also comes down to nerves and experience. Being comfortable driving right next to someone means you're less likely to make mistakes and crash, and a great racing move can not only protect you from someone behind passing you but also potentially let you pass someone you otherwise wouldn't be able to without just waiting for them to screw up and smack a wall.
I'm not going to go into too much detail on how2racecraft, a lot of this just comes down to experience, but I will discuss one of the most important concepts in racing period: BLOCKING. Blocking is, well, intentionally blocking another person's racing line so that they have to either slow down and give up a chance at passing you or ram into you and crash. This is controversial because, of course, there are some completely fair things you can do that can involve making someone slow down if they read your move wrong. The most obvious form of blocking is waiting for someone to just about catch up with you on a straight, and then when they move to the outside or the inside, following their move to force them to either slow down or bump into you. This is a dick move, don't do it. If you need to move on a straight, either do it before someone's getting close or let them pass and then move over. The clearer you can make things for other drivers, the better. When in a battle with another car, the simplest way to protect being overtaken is to move to the inside of a turn and take it tightly, thus forcing the other driver to take the long way around! This may not always work depending on the turn, but it is easy to do and doesn't cause issues for the other driver(s). There are other, even sneakier tricks but this is just a quick overview.
Practicing racecraft is vital. The best way to do this is to race, but if you don't have a race going on right now, find a buddy, go into testing in the same car and just constantly battle eachother! If one of you crashes, slow down and let them catch up. The more comfortable you get being right behind someone, just to the side of them and just ahead of them, the better you'll be as a driver.