Gaining weight will come from a variety of lifestyle factors, including your diet, exercise, genetics, sleep, etc. Perhaps most importantly (nutritionally speaking), you have to consume enough calories and protein to support your weight gain.
You will want to eat in a slight calorie surplus. This means that you are eating more calories than your body needs to maintain its weight. Just a couple or a few hundred extra calories per day on top of what you need to maintain your weight should do the trick for a slow and gradual weight gain.
Along with calories, protein is also very important. Consuming enough protein will support your weight gain and allow more of the gain to be lean mass. Protein is essential for building and maintaining muscle, as well as playing a role in other crucial processes in the body. While you don't want to consume way too much protein, you want to eat enough of it to meet your increased needs as you are exercising and trying to gain weight.
Even though diet is the main priority, your exercise is super important too! We recommend including strength training at least a few days a week if your goal is to gain muscle as well.