It is been some time since I studied abstract algebra. But I'm lately teaching some high school students "Algebra". On this level this basically means working with variables, and understanding that certain rules hold for those variables (which represent real numbers). For example:
$$ (a+b)^2 = a^2 +2ab + b^2 \\ a^na^m = a^{n+m} \\ x^2+bx+c = (x+m)(x+n) \quad \text{if } m+n=b \text{ and } mn =c $$
Now in those books that my highschool students read, they never mention for which kind of elements rules like these are true. They just state those rules.
First, I was thinking that this was just sloppy. I would have written something like for real numbers $a,b$ the following rule hold: ...
But then later I realized, wait, those rules are of course much more general. And I can also recall once having learned that there is something like an Algebra, which is some algebraic structure with some rules defined on it.
I maybe see connections that are not really here. But is the subject "Algebra" on highschool called Algebra because the rules they learn are valid for any Algebra as an algebraic structure? Is that also the reason that they don't specify that the rules are valid for real numbers, because the rules are in fact much more general (but they haven't formally learned about those more general structures, so you can not specify it).
I think the structure you want is known as a commutative ring. A ring basically lets you multiply, add and subtract things and end up with something else in the ring again.
Some examples include
The commutative property means that multiplication doesn't care about order: $a \times b = b \times a$. This is true for the first three examples, but matrices are typically the first case people properly learn where this isn't true. Polynomials will be commutative if the ring where the $a_i$ live is.
EDIT:
Commutativity is important for your top "rule": Note that when you expand the brackets you get $(a+b)^2=(a+b)(a+b)=a^2+ab+ba+b^2$. To get your rule, we must have $ab=ba$ which is precisely the commutativity part. (Oddly enough, you don't actually need commutativity of the polynomial ring for your last "rule" as $ax=xa$ for any $a$, which is why I originally didn't mention it.)
Some others have mentioned fields: these are special commutative rings where you can divide by things as well, so long as it's not zero: the real numbers and complex numbers satisfy this but the integers and polynomials don't.