Every car needs an engine, which may be developed internally or purchased from a supplier. Ferrari supplies engines for its own team (the "factory team"), but also sells engines to other teams like Toro Rosso and Sauber ("customer teams"). Engine development is extremely expensive, so amortizing the cost across several buyers makes sense. Smaller teams cannot afford to develop their own teams.
The best teams can afford to pay their own drivers lots of money, upwards of €20m/year. Some teams can only afford to pay €150k/year, and the drivers hope to prove themselves and move to a faster team after their contract is up (many top drivers came up this way). Some teams can't even afford that, and are willing to take "pay drivers," who pay the team to drive. They're not bad drivers, but they're not necessarily the best ones, either. Cutting loose a good paid driver for a worse pay driver is bad for the sport.
Participating in F1 has a unique marketing value to the right customer, and can't be replaced with just R&D or more commercials. I think it's fair to say that Ferrari wouldn't know what to do with themselves if they quit F1, as it's so much of their brand identity.