Why was it valid to invent the imaginary unit, $i$? I understand that imaginary numbers have turned out to be very useful but aren't there rules in mathematics that prevent you from inventing objects which, as far as I can see, contradict some mathematical rules?
To me, defining a number to be the $\sqrt{-1}$ is like defining a number that, when divided by zero, gives you $\pi$. Or defining a number to be "the largest prime number".
 A: Historically, there was a lot of pushback to the complex numbers (and negative numbers, and analyzing divergent series,  ...). It is hand wavy to just define things into existence.
There’s generally two approaches to this kind of thing. The first is to just define whatever you want and hope it works out. This is the most straightforward approach in textbooks lacking rigor, and when you have a reasonable intuition for what’s going on, is the quickest to get into the usage of things, but isn’t mathematically rigorous. It’s not necessary for most high schoolers to know how to prove that real or complex numbers can exist without contradictions, it’s simple enough to just assume they exist by their axioms and go from there.
The more rigorous approach is to construct the new framework in terms of an old framework and show that this new thing satisfies all the axioms that you’d want. Then, if your new framework caused contradictions, it would imply contradictions in the old one. Rationals are defined as pairs of integers. For real numbers, this is done with Dedekind cuts on top of rational numbers. For complex numbers, you define a pair of real numbers $(a,b)$ represented as $a+bi$ as a complex number and go from there showing that it satisfies all the axioms except ordering.
In fact, with the right framework and by modifying your definitions slightly, you can define a framework with infinitely small or big numbers (the hyperreal numbers), or one where there’s a number that comes after every other number (ordinals), but you need to break assumptions about numbers replacing them with something similar and define complicated constructions.
A: It's true that you can't (invent new mathematics that leads to a contradiction. (Or, better, shouldn't if you want your invention to be useful.)
Properly adding a square root of $-1$ to the set of things you call "numbers" does not lead to a contradiction. No rules are broken. The new "numbers" of the form $a+bi$ satisfy all the ordinary rules of arithmetic. None are broken.
It remains true that $-1$ has no square root in the original set, but that absence is not a "rule".
You can't add a number $q$ such that dividing it by $0$ gives you $\pi$ and still keep all the ordinary rules of arithmetic. If $q/0 = \pi$ then $q = 0 \times \pi = 0$ so $q$ wasn't a new number at all and doesn't do what you created it to do.
