The general equation for a fully-populated planet's overall output is
O = (1 + c) * (1 + p) * (P + (1 + f)*(Bf + Nf * F)) * (m * (1 + Bm + bm * Nm) + r * (1 + Br + br * Nr) + w * (1 + Bw + bw * Nw))
where O is the total output of the planet, c is the coercion modifier, p is the planet's percentile production bonus, P is the planet's flat production bonus, f is the planet's percentile food bonus, Bf is the planet's flat food bonus from all sources other than basic food improvements ('farms;' e.g. Xeno Farm), Nf is the number of farms, F is the average amount of food provided per farm, m is the fraction of planetary production allocated to manufacturing, Bm is the planet's percentile bonus to manufacturing output from all sources other than basic manufacturing improvements ('factories'), bm is the average bonus to manufacturing output per factory, Nm is the number of factories on the planet, r is the fraction of the planet's production allocated to research, w is the fraction of the planet's production allocated to wealth, and Br, br, Nr, Bw, bw, and Nw are the terms for research and wealth corresponding to the similarly-named manufacturing-related terms.
Under the assumption that you're using a fully-specialized planet (i.e. all of your planet's production goes towards a single output type), it's actually fairly easy to compute the long-term optimal build of a planet (note: 'easy' is a relative term; this is not a difficult equation to solve, but using a rule of thumb such as 1 farm per factory is even easier, though often less optimal - that is, after all, the trade-off for using a rule of thumb instead of computing what's best for the given scenario, as a rule of thumb gives you a 'good enough' solution but not necessarily the 'best' solution). In such a scenario, the equation governing the planet's output boils down to
O = (1 + c) * (1 + p) * (P + (1 + f) * (Bf + N * F)) * (1 + Bm + B * (T - (1 + A) * N))
where c is the coercion penalty, p is the planet's total production bonus, P is the planet's flat production bonus, f is the planet's total food bonus, Bf is base amount of food that the planet, N is the number of farms you build, F is the average food granted by each farm, Bm is the planet's bonus to the output type you chose from all sources other than basic factories, T is the total number of tiles available on the planet for basic factories of the type corresponding to the desired output and morale improvements and farms, A is the average number of approval structures you build per farm, and B is the average bonus you gain per basic factory of the type corresponding to the desired output. (Note that in the section covering the fully-specialized planet, 'factories' are basic output improvements, not specifically basic manufacturing improvements.) This is a concave-down parabola in N, so if you make a few simple assumptions about the average bonus per farm and per factory, you should have everything you need to solve for the value of N which maximizes the equation. That value of N can be found as
N = [(1 + Bm + B * T) * (1 + f) * F - (P + (1 + f) * Bf) * (1 + A) * B] / [2 * (1 + f)*(1 + A) * F * B]
For example, if T = 10, F = 2, B = 0.15, Bf = 5, P = 5, and all the other constants are 0, then the optimal number of farms N to build on the planet is 5.8. Obviously you cannot build exactly 5.8 farms, but either 5 or 6 farms should get you pretty close to the theoretical maximum. Note that this is long-run optimal, i.e. once the planet hits its population cap this is the configuration which maximizes the planet's potential output per turn. Ideally speaking, a fully-specialized planet will eventually neither have nor need improvements giving a bonus to an off-type output, as the population will be sufficiently high that the unmodified production allows improvements to be built or upgraded at a reasonable pace and of course once the planet is fully built the off-type improvements are no longer useful (at least until the next upgrade cycle comes around). Note that all constants should be expressed as decimals or fractions (i.e. a 10% bonus to something is 0.1 or 1/10, not 10) and should be positive if the modifier is a bonus and negative if the modifier is a penalty.
If you wanted to do so, you could use one of the above equations (or a more generalized output equation which accounts for all three output types, though maximizing the three-output equation is much more difficult) to solve for the long-run optimal planet builds. You could also use these to work out the sums required to determine the build which is optimal over the next N turns.
Other configurations can give you more output in the short term. For example, a farm is useless until the planet's population reaches a point where a farm is required to allow the population to continue to grow at the current rate, and additionally the farm will not provide its full bonus until several turns after that point as it will take time for the population to grow to the new limit; thus, there is often some number of turns over which the total output generated by the planet would have been greater had you built a factory rather than a farm, even though it may still be more optimal in the long run to build a farm.
I would suggest that you should use a rule of thumb to get a 'good enough' solution rather than working out exactly what the optimal solution for your particular planet happens to be unless you happen to like running a computation for every planet (and also every time the improvements you have access to change), but the above should give you all that you need to know if you desire to determine the optimal builds for a planet.