I am having trouble understanding the class $coNP$. We defined
$$coNP = \left\{ \overline{A} : A \in NP \right\}$$
As far as I know, a language $A$ is in $NP$ if, and only if, a non-deterministic turing machine $M$ exists, which outputs $1$ if $w \in A$ and $0$ if $w \notin A$. But if this is true, then a turing machine $M'$ must exist which simulates $M$ and inverts the output:
$$M' = \begin{cases}0 \text{ if } M = 1\\1 \text{ if } M = 0\end{cases}$$
Therefore, $w \in A \implies w \notin L(M')$ and $w \notin A \implies w \in L(M')$, meaning that $L(M') = \overline{L(M)}$.
As $M'$ works in non-deterministic polynomial time, $L(M') \in NP$, and as $L(M') = \overline{L(M)}$, $\overline{L(M)} \in NP$, but $\overline{L(M)} \in coNP$ as well. As this holds true for all languages in $NP$, why isn't $coNP = NP$?