We assume that $K\ne \emptyset$ and $U^c\ne \emptyset$. If $C\subseteq S$ is a Borel set and $C\ne \emptyset$, then the function $x\mapsto d(x,C)$ is Lipschitz. Indeed, we have $d(x,C)\le d(x,y) + d(y,C)$, i.e., $d(x,C) - d(y,C)\le d(x,y)$ for all $x,y\in S$, and analogously, $d(y,C) - d(x,C)\le d(x,y)$, so $|d(x,C) - d(y,C)|\le d(x,y)$ for all $x,y\in S$.
The set $K$ is compact, $U$ is open and $K\subseteq U$, so we have $\delta := d(K,U^c) > 0$. Thus, $d(x,U^c) + d(x,K) \ge \delta > 0$ for all $x\in S$, so $f$ is well-defined.
Now let $g(x) = d(x,U^c)$ and $h(x) = d(x,U^c) + d(x,K)$ for $x\in S$, i.e. $f(x) = g(x)/h(x)$. Then obviously $g(x)\le h(x)$, $h(x)\ge \delta$ for all $x\in S$, and the functions $g$ and $h$ are Lipschitz continuous with Lipschitz constants less than or equal to $1$ and $2$, respectively. Therefore, for all $x,y \in S$, we have \begin{align*} |f(x) - f(y)| &= \Big| \frac{g(x)}{h(x)} - \frac{g(y)}{h(y)} \Big| = \frac{|g(x)h(y) - g(y)h(x)|}{h(x)h(y)}\\ &\le \frac{|g(x)h(y) - g(x)h(x)| + |g(x)h(x) - g(y)h(x)|}{h(x)h(y)}\\ &\le \frac{g(x)\cdot 2d(x,y) + h(x)d(x,y)}{h(x)h(y)}\\ &\le \frac{3h(x)}{h(x)h(y)}d(x,y) \le \frac{3}{\delta}d(x,y), \end{align*} so $f$ is Lipschitz.
If $x\in K$, then $f(x) = \frac{d(x,U^c)}{d(x,U^c)} = 1$, and if $x\in U^c$, then $f(x) = \frac{0}{d(x,K)} = 0$. Since $K\subseteq A\subseteq U$, this implies that $f - I_A = 0$ on $K$ and on $U^c$, and we have $|f-I_A|\le 1$ on $U\setminus K$ because $0\le f\le 1$ and $0\le I_A\le 1$. Hence, \[ \int |f-I_A|^p d\mu = \int_{U\setminus K}|f-I_A|^p d\mu \le \mu(U\setminus K) < \epsilon. \]