This question for connected $G$ is exactly asking for a "strong approximation" property for the construction of torsors, and that generally fails for both tori and connected semisimple groups beyond the simply connected case (due to various phenomena explained by class field theory), and much deeper is the affirmative answer when $G$ is a connected semisimple group that is simply connected.
Michael Stoll's counterexample for $G = \mu_2$ over $K = \mathbf{Q}$ generalizes to give a negative answer in the disconnected case over every number field for $G = \mu_n$ and $f$ the $n$th-power endomorphism of $\mathbf{G}_m$ with any $n > 1$, again explained by class field theory.
To see what is going on, we first translate the problem into more hands-on terms in a more general setting without connectedness conditions.
Let $K$ be a global field and $G$ a smooth $K$-group of finite type. (I want to incorporate the case of abelian varieties into the preliminary discussion.) The question for $G$ concerns the pullback of $f$ along $K$-morphisms $x: {\rm{Spec}}(\mathbf{A}_K) \rightarrow X$, which is to say that one is given a $G$-torsor $E$ over ${\rm{Spec}}(\mathbf{A}_K)$ and is asking if it is split by twisting along a $G$-torsor over $K$. Since you emphasize that you allow $X$ to be an arbitrary $K$-scheme (so not even assuming it to be of finite type over $K$), what you are asking is equivalent to the question of whether or not ${\rm{H}}^1(K,G) \rightarrow {\rm{H}}^1(\mathbf{A}_K,G)$ is surjective, where we are working with the etale topology (since $G$ is $K$-smooth).
The adelic H$^1$ enforces an "almost everywhere integral" condition that is not present when considering $\prod_v {\rm{H}}^1(K_v,G)$. To explain this in precise terms (and then to address the surjectivity question), first observe that by "spreading out" of the connected-etale sequence for $G$, we may choose a finite set $S_0$ of places of $K$ large enough so that $G$ extends to a smooth separated $O_{K,S_0}$-group $\mathscr{G}$ of finite type (even affine if $G$ is affine) which fits into a short exact sequence
$$1 \rightarrow \mathscr{G}^0 \rightarrow \mathscr{G} \rightarrow \Gamma \rightarrow 1$$
where $\mathscr{G}^0$ is a smooth closed $O_{K,S_0}$-subgroup with connected fibers and $\Gamma$ is a finite etale $O_{K,S_0}$-group. Since $\mathbf{A}_K$ is the direct limit of its subrings $\mathbf{A}_{K,S} = (\prod_{v \in S} K_v) \times \prod_{v \not\in S} O_{K,v}$ as $S$ varies through the finite sets of places of $K$ containing $S_0$, by standard limit arguments we have ${\rm{H}}^1(\mathbf{A}_K,G) = \varinjlim {\rm{H}}^1(\mathbf{A}_{K,S},\mathscr{G})$. But $\mathbf{A}_{K,S}$ is a direct product of local henselian rings, so a cofinal system of etale covers of its spectrum is given by precisely the spectra of the product rings $R = \prod_v R_v$ where $R_v$ is a local finite etale algebra over $K_v$ for $v \in S$ and over $O_{K,v}$ for $v\not\in S$ with these local algebras of bounded degree as $v$ varies (see Lemma 7.5.5 in the paper "Finiteness theorems for algebraic groups over function fields" in Compositio 148). This provides a natural injection
$${\rm{H}}^1(\mathbf{A}_{K,S},\mathscr{G}) = \varinjlim_R {\rm{H}}^1(R/\mathbf{A}^1_{K,S}, \mathscr{G}) \rightarrow \prod_{v \in S} {\rm{H}}^1(K_v,G) \times \prod_{v \not\in S} {\rm{H}}^1(O_{K,v},\mathscr{G})$$
whose image consists of precisely the collections $(E_v)_v$ of classes $E_v$ split by a finite etale cover of bounded degree over the local $v$-factor ring for all $v$.
This uniform boundedness condition is automatic (so the degree-1 ${\mathbf{A}}_{K,S}$-cohomology set is identified with the indicated direct product). To prove this, first recall that for $v\not\in S$, Lang's theorem over the finite residue field of $O_{K,v}$ implies the triviality of torsors for smooth $O_{K,v}$-groups with connected fibers. We may apply this to the $O_{K,v}$-groups arising by "twisting" $\mathscr{G}^0_{O_{K,v}}$ by its torsors over $O_{K,v}$ (i.e., automorphism schemes of such torsors). Hence, the twisting method for torsors (for the etale topology) gives that for $v \not\in S$ the natural map ${\rm{H}}^1(O_{K,v},\mathscr{G}) \rightarrow {\rm{H}}^1(O_{K,v},\Gamma)$ is injective. Since $\Gamma$ is finite etale (so likewise for its torsors over $O_{K,v}$), it follows via the valuative criterion for properness (in the easy case of finite morphisms) that ${\rm{H}}^1(O_{K,v},\Gamma)$ restricts isomorphically onto the set ${\rm{H}}^1(K_v^{\rm{un}}/K_v,\Gamma)$ of classes in ${\rm{H}}^1(K_v,\Gamma)$ split by a finite unramified extension of $K_v$; note also that $\Gamma_{K_v}$ is itself split by an unramified extension of $K_v$. But every class in ${\rm{H}}^1(K_v^{\rm{un}}/K_v,\Gamma)$ is an "unramified" finite etale $K_v$-scheme of degree equal to the order of $\Gamma$ and hence is split by an unramified extension of degree bounded solely in terms of the order of $\Gamma$. This provides the desired uniform boundedness of local splitting degrees as $v$ varies.
To summarize, $${\rm{H}}^1(\mathbf{A}_K,G) = \varinjlim_{S \supset S_0} \prod_{v \in S} {\rm{H}}^1(K_v,G) \times \prod_{v \not\in S} {\rm{H}}^1(O_{K,v},\mathscr{G})$$ and via restriction we have
${\rm{H}}^1(O_{K,v},\mathscr{G}) \subset {\rm{H}}^1(K_v^{\rm{un}}/K_v,G)$
for all $v\not\in S_0$. This latter injective restriction map is bijective when $\mathscr{G}$ is $O_{K,v}$-proper (as can be arranged to happen when $G$ is $K$-proper, such as $K$-finite), but it is generally not an equality; e.g., if $\mathscr{G}_{O_{K,v}}$ has connected fibers (as happens when $G$ is connected) then the $O_{K,v}$-cohomology set vanishes by Lang's theorem but the unramified cohomology set is generally non-trivial (e.g.,for $G = {\rm{PGL}}_n$ is recovers the $n$-torsion in ${\rm{Br}}(K_v)$).
Hence, we always have ${\rm{H}}^1(\mathbf{A}_K,G) \subset \prod_v {\rm{H}}^1(K_v,G)$ and this lies inside the set of classes $(c_v)_v$ that are unramified at all but finitely many places but is generally much smaller than that: it is characterized by an "almost everywhere integral" condition relative to a fixed choice of smooth separated finite type $S_0$-integral model $\mathscr{G}$ as above.
In the special case $G = \mathbf{G}_m$ over $K$ we may take $\mathscr{G} = \mathbf{G}_m$, so it follows that ${\rm{Pic}}(\mathbf{A}_K) = 1$.
That's enough of generalities, so we now return to the question posed. In the special case where $f:Z \rightarrow X$ is the $n$th-power endomorphism of $\mathbf{G}_m$ over a number field $K$, the question is asking about surjectivity of ${\rm{H}}^1(K,\mu_n) \rightarrow {\rm{H}}^1(\mathbf{A}_K,\mu_n)$, which is to say surjectivity of $K^{\times}/(K^{\times})^n \rightarrow \mathbf{A}_K^{\times}/(\mathbf{A}_K^{\times})^n$. (Here we have computed the $\mu_n$-cohomology with the Kummer sequence, using the Pic vanishes for $\mathbf{A}_K$ too.) In other words, is the $n$th-power endomorphism of the idele class group $\mathbf{A}_K^{\times}/K^{\times}$ surjective? Specializing to $K = \mathbf{Q}$ and $n=2$ we recover the counterexample provided by Stoll, and more generally for every number field $K$ this fails for every $n > 1$ since the quotient of $\mathbf{A}_K^{\times}/K^{\times}$ modulo its subgroup of infinitely divisible elements is the abelianized Galois group of $K$ (by class field theory) and that quotient is not $n$-divisible for any $n > 1$ due to the existence of finite abelian extensions of prime degree dividing $n$ (e.g., using cyclotomic extensions).
Now we assume $G$ is connected and have to address if ${\rm{H}}^1(K,G) \rightarrow {\rm{H}}^1(\mathbf{A}_K,G)$ is surjective. As above, we may build a smooth separated finite type $O_{K,S_0}$-group $\mathscr{G}$ with generic fiber $G$ and with all fibers connected, so we have seen that ${\rm{H}}^1(\mathbf{A}_K,G)$ consists of exactly the set $\coprod {\rm{H}}^1(K_v,G)$ of collections $(c_v)_v$ of local classes that are trivial for all but finitely many $v$. In other words, this is precisely the question of whether a collection of $G$-torsors $E_v$ over $K_v$ which are trivial for all but finitely many $v$ can be assembled into a global torsor $E$ over $K$. One may regard this as a "strong approximation" property for the construction of torsors.
Now assume $K$ is a number field and $G$ is affine, as in the question posed.
By Mostow we have $G = H \ltimes U$ for reductive $H$ and unipotent $U$, and an elementary Galois-twisting argument shows that ${\rm{H}}^1(K,H) \rightarrow {\rm{H}}^1(K,G)$ is bijective (since any $K$-form of $U$ is unipotent and smooth connected unipotent $K$-groups have vanishing degree-1 cohomology since ${\rm{char}}(K)=0$). Thus, the problem for connected affine $G$ is equivalent to that for its maximal reductive quotient, so now assume $G$ is reductive.
For tori we get counterexamples via consideration of norms. Namely, let $T$ be the norm-1 torus $$T = \ker({\rm{R}}_{K'/K}(\mathbf{G}_m) \twoheadrightarrow \mathbf{G}_m)$$ for finite separable $K'/K$. Then by Hilbert 90 we see that the problem for $T$ is exactly the question of whether or not the norm map
$$\mathbf{A}_{K'}^{\times}/{K'}^{\times} \rightarrow \mathbf{A}_K^{\times}/K^{\times}$$
is surjective. Infinitely divisible elements in the target are certainly hit, so to make a counterexample it suffices for surjectivity to fail modulo the subgroups of infinitely divisible elements. By class field theory that induced map is exactly the natural map $\Gamma_{K'}^{\rm{ab}} \rightarrow \Gamma_K^{\rm{ab}}$ between abelianized Galois groups. This is surjective if and only if every finite abelian extension $F/K$ is linearly disjoint from $K'$ over $K$, and that fails precisely when $K'$ contains a nontrivial finite abelian extension of $K$.
Hence, the "last chance" for an affirmative answer is in the connected semisimple case. Here one encounters counterexamples when $G$ is not simply connected. For example, if $G = {\rm{PGL}}_n$ with $n > 1$ then ${\rm{H}}^1(K,G) = {\rm{Br}}(K)[n]$ and likewise for $K_v$, and by class field theory the natural map ${\rm{Br}}(K)[n] \rightarrow \oplus_v {\rm{Br}}(K_v)[n]$ is never surjective for $n > 1$.
Finally, suppose $G$ is simply connected too. Then we get a positive answer! Indeed, by the deep Kneser-Bruhat-Tits theorem we have ${\rm{H}}^1(K_v,G)=1$ for all non-archimedean $v$, so the problem is surjectivity of ${\rm{H}}^1(K,G) \rightarrow \prod_{v|\infty} {\rm{H}}^1(K_v,G)$ for such $G$. This latter map is actually surjective for all connected linear algebraic groups over number fields (see Proposition 6.17 in the book of Platanov-Rapinchuk, which rests on hard work with tori).