[proofplan]
Throughout the proof, $P(z)$ denotes the positive squarefree integer fixed in the statement, $S(A,P,z)$ denotes the number of elements of $A$ coprime to $P(z)$, and $A_d$ denotes the subset of $A$ consisting of multiples of $d$. The proof is obtained by applying the defining upper bound sieve inequality separately to each element $a \in A$ and then summing over $A$. The divisibility condition $d \mid \gcd(a,P(z))$ is exactly the same as the simultaneous conditions $d \mid P(z)$ and $d \mid a$, so interchanging the two finite sums produces the counts $|A_d|$. The second estimate is then just substitution of the decomposition $|A_d| = g(d)X + r_d$ into the first estimate.
[/proofplan]
[step:Apply the upper bound sieve inequality to each element of $A$]
For a set $E$ and an integer $m$, let $\mathbb{1}_E(m)$ denote the indicator of membership in $E$. In particular, $\mathbb{1}_{\{\gcd(a,P(z))=1\}}$ denotes the number equal to $1$ when $\gcd(a,P(z))=1$ and equal to $0$ otherwise. By definition,
\begin{align*}
S(A,P,z) := |\{a \in A : \gcd(a,P(z))=1\}| = \sum_{a \in A} \mathbb{1}_{\{\gcd(a,P(z))=1\}}.
\end{align*}
For each $a \in A$, the defining property of the upper bound sieve weights gives
\begin{align*}
\mathbb{1}_{\{\gcd(a,P(z))=1\}} \leq \sum_{d \mid \gcd(a,P(z))} \lambda_d.
\end{align*}
Summing this inequality over all $a \in A$ gives
\begin{align*}
S(A,P,z) = \sum_{a \in A} \mathbb{1}_{\{\gcd(a,P(z))=1\}} \leq \sum_{a \in A} \sum_{d \mid \gcd(a,P(z))} \lambda_d.
\end{align*}
[guided]
Let $\mathbb{1}_E(m)$ denote the indicator of membership in a set $E$: it is $1$ if $m \in E$ and $0$ otherwise. Thus $\mathbb{1}_{\{\gcd(a,P(z))=1\}}$ is the indicator of the event that $a$ is coprime to $P(z)$. The sieve counting function is
\begin{align*}
S(A,P,z) := |\{a \in A : \gcd(a,P(z))=1\}| = \sum_{a \in A} \mathbb{1}_{\{\gcd(a,P(z))=1\}}.
\end{align*}
The point of the upper bound sieve weights is that they dominate this indicator. For a fixed integer $a \in A$, the hypothesis gives
\begin{align*}
\mathbb{1}_{\{\gcd(a,P(z))=1\}} \leq \sum_{d \mid \gcd(a,P(z))} \lambda_d.
\end{align*}
This inequality is valid because $a$ is an integer, and the upper bound sieve condition was assumed for every integer $n$.
Now sum over the finite set $A$. Since $A$ is finite, no convergence or measurability issue is involved:
\begin{align*}
\sum_{a \in A} \mathbb{1}_{\{\gcd(a,P(z))=1\}} \leq \sum_{a \in A} \sum_{d \mid \gcd(a,P(z))} \lambda_d.
\end{align*}
By the definition of $S(A,P,z)$, the left-hand side counts exactly those elements $a \in A$ satisfying $\gcd(a,P(z))=1$. Hence
\begin{align*}
S(A,P,z) = \sum_{a \in A} \mathbb{1}_{\{\gcd(a,P(z))=1\}} \leq \sum_{a \in A} \sum_{d \mid \gcd(a,P(z))} \lambda_d.
\end{align*}
[/guided]
[/step]
[step:Rewrite the summed divisor condition as the counts $|A_d|$]
For integers $a$ and positive integers $d$, the condition $d \mid \gcd(a,P(z))$ is equivalent to the two simultaneous conditions $d \mid P(z)$ and $d \mid a$. Therefore, because both $A$ and the divisor set of $P(z)$ are finite,
\begin{align*}
\sum_{a \in A} \sum_{d \mid \gcd(a,P(z))} \lambda_d = \sum_{d \mid P(z)} \lambda_d |\{a \in A : d \mid a\}|.
\end{align*}
By the definition of $A_d$, this becomes
\begin{align*}
\sum_{a \in A} \sum_{d \mid \gcd(a,P(z))} \lambda_d = \sum_{d \mid P(z)} \lambda_d |A_d|.
\end{align*}
Combining this identity with the preceding inequality gives
\begin{align*}
S(A,P,z) \leq \sum_{d \mid P(z)} \lambda_d |A_d|.
\end{align*}
[guided]
We now convert the sum over elements $a \in A$ into a sum over divisors of $P(z)$. Fix an integer $a \in A$ and a positive integer $d$. By the defining property of the greatest common divisor, the divisibility relation $d \mid \gcd(a,P(z))$ holds exactly when $d \mid a$ and $d \mid P(z)$ both hold. Thus the inner sum over divisors of $\gcd(a,P(z))$ may be viewed as a sum over those divisors $d$ of $P(z)$ that also divide $a$.
Since $A$ is finite and $P(z)$ is a fixed integer, the set of pairs
$\{(a,d) : a \in A, d \mid P(z), d \mid a\}$ is finite. Hence changing the order of summation is just finite rearrangement of terms, giving
\begin{align*}
\sum_{a \in A} \sum_{d \mid \gcd(a,P(z))} \lambda_d = \sum_{d \mid P(z)} \lambda_d |\{a \in A : d \mid a\}|.
\end{align*}
The set $A_d$ denotes $\{a \in A : d \mid a\}$, so the cardinality in the last display is $|A_d|$. Therefore
\begin{align*}
\sum_{a \in A} \sum_{d \mid \gcd(a,P(z))} \lambda_d = \sum_{d \mid P(z)} \lambda_d |A_d|.
\end{align*}
Combining this identity with the bound obtained in the previous step yields
\begin{align*}
S(A,P,z) \leq \sum_{d \mid P(z)} \lambda_d |A_d|.
\end{align*}
[/guided]
[/step]
[step:Substitute the local density decomposition]
Because $P(z)$ is squarefree, every positive divisor $d$ of $P(z)$ is squarefree. Hence the stated local density hypothesis gives $|A_d| = g(d)X + r_d$ for every divisor $d$ of $P(z)$. Substituting this identity into the weighted upper bound gives
\begin{align*}
S(A,P,z) \leq \sum_{d \mid P(z)} \lambda_d (g(d)X + r_d).
\end{align*}
Using finite distributivity of sums,
\begin{align*}
\sum_{d \mid P(z)} \lambda_d (g(d)X + r_d) = X\sum_{d \mid P(z)} \lambda_d g(d) + \sum_{d \mid P(z)} \lambda_d r_d.
\end{align*}
Therefore
\begin{align*}
S(A,P,z) \leq X\sum_{d \mid P(z)} \lambda_d g(d) + \sum_{d \mid P(z)} \lambda_d r_d.
\end{align*}
This is the claimed separation into the expected main term and the weighted remainder term.
[guided]
The first estimate has reduced the sieve problem to understanding the weighted divisor sum $\sum_{d \mid P(z)} \lambda_d |A_d|$. Since $P(z)$ is squarefree, every positive divisor $d$ of $P(z)$ is squarefree. Therefore the hypothesis supplies a decomposition of each local count into a main term and an error term: for every divisor $d$ of $P(z)$ under consideration,
\begin{align*}
|A_d| = g(d)X + r_d.
\end{align*}
Substituting this equality into the weighted upper bound gives
\begin{align*}
S(A,P,z) \leq \sum_{d \mid P(z)} \lambda_d (g(d)X + r_d).
\end{align*}
The sum is finite because $P(z)$ has finitely many positive divisors. We may therefore distribute $\lambda_d$ over the two summands and separate the resulting finite sum into two finite sums:
\begin{align*}
\sum_{d \mid P(z)} \lambda_d (g(d)X + r_d) = X\sum_{d \mid P(z)} \lambda_d g(d) + \sum_{d \mid P(z)} \lambda_d r_d.
\end{align*}
Combining the last two displayed formulas gives
\begin{align*}
S(A,P,z) \leq X\sum_{d \mid P(z)} \lambda_d g(d) + \sum_{d \mid P(z)} \lambda_d r_d.
\end{align*}
This proves the asserted decomposition of the upper bound into the expected main term and the weighted remainder term.
[/guided]
[/step]