It's down to monies. There's no hard limit on spending (yet) so larger teams can and do invest more. On top of that smaller teams have to buy some of the components from the larger ones and integrate those into their designs. This is bound to be less effective than doing integrated, end to end design like Mercedes does.
Ferrari have a historical place in the sport, and have oversized power. They are also a racing company that builds luxury sports road cars to fund their racing ambitions (every other car company is a road car company that uses F1 as marketing).
Yet, they have been shooting themselves in the foot for the last few years, even though on paper their cars were faster than Mercedes'.
It's not just about money, though money is the biggest factor. It's about having a good organization, good decision making, and good DRIVERS.