I've been going to the gym fairly consistently for about 20 years, here's my personal experience for what it's worth.
[the real answer is: it's complicated, no one knows for sure and it will probably depend on personal circumstances anyway - like security, in nutrition anyone who says they know something 100% is talking out of their ass, we just don't know enough yet]
For more than 30 years already, body building community has been saying that you need more than a certain amount of calories, (especially from protein) in your diet to increase muscle mass. The first thing I read against sugar, was in Arnold Swartzeneger's biography circa 1997.
Slowly over the decades the general public seems to have come round to that conclusion too - more protein seems to be a good thing for your health and strength, sugar seems to be bad.
Personally, at around 18 years old I started taking protein powders, not consistently because I couldn't afford it, roughly a few months on, a few months off. And they do appear to work. They are very easy to prepare and drink, some taste delicious. Downsides: some of them contain a lot of sugar, and as a differentiator a lot contain various custom chemical compounds which claim to give you and extra few percent effect - I strongly suspect this is mainly bulltish, potentially harmful to your health, or even if it does give you an extra few percent, it's not worth the extra money.
Here's the killer though: the moment you stop taking protein powder, the muscle seems to disappear.
I don't know why. Maybe when you take protein powder you get used to eating less food, and this stays with you when you stop taking the powder. Maybe it's because I was younger then. Maybe it's a deliberate short term boost by the manufacturer to dissuade people from giving up their product (not sure we know enough to do that, but it's possible I guess)
In any case, over the years I have switched to getting extra protein simply by eating a lot of food, it's probably healthier because it's more varied, the fact that the food also includes salads and beans and so on means that I am getting all sorts of extra micronutrients we don't know a lot about. Downsides to this is that depending on where you live and what your lifestyle is like, it can be difficult to just eat twice as much as the average person does. Lack of appetite can become a problem if the food is not delicious - for that I would really recommend staying away from anything sweet at least until you've finished all the protein on your plate, e.g. orange juice.
The amazing thing about building muscle mass from regular food (for me at least) is that the muscle doesn't immediately disappear if I go on holiday for two weeks, or even just don't each much for a month. Granted, I can't be sure that isn't just because I am older.
Another thing I've come I realise is that lots of food in general seems to work, even when it's not particularly focused on proteins like chicken, eggs etc. I've met vegans who are still fairly buff which is something I would never have believed was possible 10 years ago.
Maybe it goes without saying but food is only one part of the challenge. To get muscle mass you need three things usually (and if one is missing, gains don't come): Food&water, Resistance exercise, Sleep&rest
What the other person said about compound exercises is really true. Squats in particular are some kind of magic exercise, they seem to boost your appetite and drive muscle mass everywhere in you body. If the gym you use doesn't have a squat rack and lots of free weights, look for another gym. It's especially important to be safe and use correct technique for squats.
The main thing about gains is just don't give up searching for what works for you. Eventually it clicks.