How to find the profit-maximizing output level? Table 11 In this post, step 5 states the profit-maximizing output level is quantity 5. But in this case, $p=28$, $MC=30$, $p\neq MC$. Why it is the profit-maximizing output level?

Step 4 states the output level where price equals the marginal cost is the output level that maximizes profits. If so, both $q=4$ and $q=5$, the profit is $\$40$. Why we don't choose $q = 4$ as the profit-maximizing level?
Actually 4 in this case would be completely valid answer since the net profit at both quantity 4 and quantity 5 is equal  $\Pi(q=4)=\Pi(q=5)=40$. I would even argue 4 is potentially better answer.

I think that the reasoning of whoever made that textbook was that given the profits are equal whichever quantity has marginal costs closer to price should be preferred.

In this case the marginal costs at $q=4$ are 17 and $q=5$ are 30 and 30 is just more closer to 28 than 17. Again, I would not say that necessarily the best answer to discrete problem like this, but my guess is they want to teach to always look at point where price is the closest to MC since in optimum it should be equal.

I would not read too much into this exercise. At least this chapter of that online resource is at high-school or at best very simple undergraduate level. At such low level often rigor is sacrificed for the sake of making the material not too difficult in order to not discourage young students. In your future courses/self-study you will get more precision and nuance.