An invariant to keep in mind: the number of unknowns in the partial fractions form is always equal to the degree of the denominator in the rational function we're transforming.
So then, to handle $\frac{\text{something}}{x^2}$, we need two variables - either $\frac{Ax+B}{x^2}$ or $\frac{A}{x}+\frac{B}{x^2}$. The latter form is more useful for our end goal of integrating this, so that's what we use.
The theorem behind partial fractions, that tells us the whole scheme works, is a theorem of linear algebra. We're matching two vector spaces; the proper rational functions with a specific denominator, and the sums of certain proper rational functions with denominators among the factors of that denominator. That's why the number of unknowns will match the degree - the dimensions of the two spaces are the same.
For relatively prime factors, like $(x-1)$ and $(x+1)$, we can split their terms apart just fine; $\frac1{x-1}$ and $\frac1{x+1}$ will be linearly independent, and the sums $\frac{A}{x-1}+\frac{B}{x-1}$ will cover all possibilities with denominator $\frac1{(x-1)(x+1)}$. On the other hand, repeated factors cause trouble; if we're trying to break up $\frac{\text{something}}{(x-1)(x-1)}$, we can't just use $\frac{A}{x-1}+\frac{B}{x-1}$ because $\frac{1}{x-1}$ and $\frac1{x-1}$ are linearly dependent. In order to restore the independence we need, we go up a degree to $\frac{A}{x-1}$ and $\frac{B}{(x-1)^2}$. The more times we repeat a factor, the more degrees we go up.
In example 4, also, how do we know that $x-1$ is a factor?
A theorem of basic algebra - the "factor theorem". If $a$ is a root of the equation $f(x)=0$, then $(x-a)$ is a factor of $f(x)$, and vice versa. Why? Divide by $x-a$ to get a quotient and remainder; $f(x)=Q(x)\cdot(x-a)+R$, where the degree of $R$ is less than the degree $1$ of $(x-a)$, hence $R$ is a constant. Evaluating at $a$, $f(a)=R$, so the remainder is zero if and only if $f=0$ has a root there.