This logic is something that I've seen many beginners struggle with, so I will try to give a thorough answer.
The optimal price is not derived by maximizing revenue, as you suggest, but rather by maximizing profits. So you want to maximize revenue minus costs.
If you were to include costs in your approach you would come to the correct conclusion that the solution is "Marginal Revenue = Marginal costs". Mathematically, maximizing Profit = Revenue - Costs means taking the derivative and setting it to zero. The derivative of revenue and costs are marginal revenue and marginal cost respectively.
This rule is always true. Nevertheless, you must take the type of competition into account. Perfect competition has a different revenue expression than a monopoly.
Suppose we have perfect competition. This means the firm is a price taker. In turn, the firm cannot take the demand function into account when making its decision (by optimizing). So the firm maximizes:
$Profit = p*y - c*y$.
The solution is $p=c$.
Taking the first order condition will hence give you the famous "price = marginal cost" rule. In this case, the marginal revenue is just $p$. Here, the revenue for each extra unit sold is just whatever that unit costs. There could be price effects from selling an additional unit, since a larger supply means a lower price. Nevertheless, the optimizing firm in perfect competition cannot take such price effects into account. It must take the price as given.
The monopolist, however, does not take the price as given, but can choose it. This is done by taking the (inverse) demand function into account. I find it easier to use inverse demand, which is $P(y)$. You can find this by rearranging your demand function, which is $D(p)=y(p)$. We have to maximize:
$Profit = P(y)*y - c*y$.
The solution here is:
$P(y) + P'(y)* y = c$
Therefore, we have marginal revenue equals marginal cost. This is what I believe you were attempting to do and it only works for monopolies.