The property one uses is that two polynomials
$$ p(x) = a_n x^n + \dots + a_1 x + a_0, \\
q(x) = b_m x^m + \dots + b_1 x + b_0 $$
are equal to each other if and only if $a_n = b_n$ for all $n \in \mathbb{N}_0$. In other words, two polynomials are equal if and only if their coefficients are equal (and in particular, they must be of the same degree).
How one would justify this actually depends on how you think of polynomials. From an algebraic point of view, a polynomial is a formal expression of the form $p(x) = a_n x^n + \dots + a_1 x + a_0$ and two polynomials are defined to be equal if their coefficients are equal.
However, an alternative approach would be to define a polynomial as a function $p \colon \mathbb{R} \rightarrow \mathbb{R}$ for which there exists real numbers $a_0,\dots,a_n$ such that $p(x) = a_nx^n + \dots + a_1 x + a_0$ for all $x \in \mathbb{R}$. From this point of view, it is not clear that if you have two polynomial functions $p(x),q(x)$ such that $p(x) = q(x)$ for all $x \in \mathbb{R}$ then their coefficients must be equal (and in fact this is not true if one replaces $\mathbb{R}$ with an arbitrary field or ring).
To prove this, one can give an algebraic argument using the Vandermonde determinant but for real polynomials, it is easier to note that the $k$-coefficient of $p(x) = a_n x^n + \dots + a_1 x + a_0$ is given by $a_k = \frac{p^{(k)}(0)}{k!}$ where $p^{(k)}(0)$ is the $k$-th derivative of $p$ at $x = 0$. Then if two polynomials are equal as functions, it is clear that all their derivatives of all orders must be equal and so the coefficients must be equal.