We'll heavilly use the following fact: for a fixed public modulus $n$ product of distinct primes, a pair of integers $(e,d)$ forms a matching pair of RSA exponents [that is, with $c\mapsto c^d\bmod n\,=\,m$ capable of reliably deciphering any plaintext $m$ in $[0,n)$ enciphered per $m\mapsto m^e\bmod n\,=\,c$ ] if and only if$$e\cdot d\equiv1\pmod{\lambda(n)}$$where $\lambda$ is the Carmichael function. That can be shown to follow from the definition of $\lambda(n)$ as the smallest strictly positive integer $y$ such that $m^y\equiv 1\pmod n$ for all $m\in\mathbb Z^*$. This holds regardless of the sign of $d$.
It follows that $t$ of step 1 of the question's algorithm is such that exists $k\in \mathbb Z$ with $t=k\cdot\lambda(n)$.
If the algorithm finds $f=1$ at the first execution of step 2, it thus holds $r\cdot k\cdot\lambda(n)+s\cdot e_1=1$, hence $s\cdot e_1=1+(-r\cdot k)\cdot \lambda(n)$, hence when the algorithm sets $d'_1=s$ in step 3 it holds $e_1\cdot d'_1\equiv1\pmod{\lambda(n)}$. Applying the first fact, $(e_1,d'_1)$ is a matching pair of RSA exponents for public modulus $n$. If we want $d'_1$ to be non-negative we can make $d'_1=s\bmod t$, which by definition is in range $[0,t)$ and also such that $e_1\cdot d'_1\equiv1\pmod{\lambda(n)}$.
Things get wrong when $f\ne1$ at the first execution of step 2. Often $s$ won't divides $t$ in step 4, preventing application of the algorithm as is. Example: $p=13$, $q=19$, $n=247$, $\varphi(n)=216$, $\lambda(n)=36$, $e_1=91$, $e_2=25$, $d_1=19$, $d_2=121$, $t=3024$, $r=-4$, $s=133$, $f=7$, $t/s=432/19\not\in\mathbb Z$.
Changing $t:=\frac t s$ to $t:=\frac t f$ in step 4 insures divisibility, and leaves the algorithm working. Argument: $f$ divides $e_1$, and $\gcd(e_1,\lambda(n))=1$, thus $f$ is coprime with $\lambda(n)$, thus we restart steps 2 with $t$ still a multiple of $\lambda(n)$.
Alternatively: given $(n,e_2,d_2)$ the adversary can factor $n$ (see this) and from that get $\hat{d_1}=e^{-1}\bmod\lambda(n)$ matching $(n,e_1)$, typically with $\hat{d_1}$ smaller/faster than $d'_1$; or get a working private key in the form allowing CRT operation thus even faster decryption or signature.