How to generalize "is an element of a subset"? In category $\mathbf{Set}$, we can take a set $C$, construct its powerset $\mathcal{P}(C)$ and then given another set $D$, we can take a function $f:D \to \mathcal{P}(C)$ and ask, for a given $c \in C$ and $d \in D$, is $c \in f(d)$.
To get away from set-theoretic notation (and generalize the notion to other categories), we can replace $c \in C$ with $c:1 \to C$, where $1$ is a terminal object. Also, we can replace $\mathcal{P}(C)$ with power object $\Omega^C$ (together with monomorphism $\in_C \hookrightarrow C \times \Omega^C$).
But I cannot figure out of there is a way to state "$c \in f(d)$". Obviously $c$ is not a morphism with codomain $\Omega^C$, so I can't simply state $c:1 \to \Omega^C$, so it has to be some kind of composition or a diagram.
I've been thinking about some pullback square like the following:
$\require{AMScd}
\begin{CD}
r @>>> \in_C;\\
@VVV @VVV \\
C \times D @>{id \times f}>> C \times \Omega^C;
\end{CD}$
but couldn't think of what the projections would be. I can't use the definition of power object since I would need to find $r$, and the definition says that for a given $r$ there exists $f$ such that the diagram is a pullback, not the other way around.
 A: This all becomes more clear if we spell out a bit the dictionary between classical logic concepts and the "internal logic" of a category.

*

*An object $X$ is a set or a type.

*An arrow $x\colon 1\to X$ is an element of $X$.

*An arrow $t\colon Y\to X$ as a term of type $X$ or a generalized element of $X$ in context $Y$. In other words, if you give me an element of $Y$, I can "plug it in to $t$" (compose with $t$) to get an element of $X$.

*A subobject of $X$ is a predicate on $X$ or a property of elements of $X$.

Given $X$ and a predicate $P$ on $X$, represented by the monic arrow $p\colon P\hookrightarrow X$, we say that an element $x\colon 1\to X$ (or a generalized element $x\colon Y\to X$) satisfies the property $P$ if the arrow $x$ factors through $p$, i.e., $x = p\circ f$ for some $f\colon 1\to P$ (or $f\colon Y\to P$ in the generalized element case).
Now (in the notation of ordinary logic), given a predicate $P(x)$ on $X$ and a term $t(y)$ of type $X$ in context $Y$, we should be able to substitute the term into the predicate to obtain a predicate $P(t(y))$ on $Y$. This is accomplished categorically by pulling back the monic arrow $p\colon P\hookrightarrow X$ along the arrow $t\colon Y\to X$ to get a monic arrow $p'\colon P'\hookrightarrow Y$.
$$\require{AMScd}
\begin{CD}
P' @>>> P\\
@VV{p'}V @VV{p}V \\
Y @>{t}>> X
\end{CD}$$
You can check using the universal property of the pullback that an arrow $y\colon Z\to Y$ factors through $p'$ if and only if the composite $t\circ y\colon Z\to X$ factors through $P$, i.e., $P'(y)$ if and only if $P(t(y))$.

Ok, now let's specialize to your case. The predicate $\in_C$ is a property of a pair $(c,A)$, where $c$ is an element of $C$ and $A$ is an element of the power object $\Omega^C$. This is why $\in_C\hookrightarrow C\times \Omega^C$ is a subobject, i.e. a predicate on $C\times \Omega^C$.
You have a function $f\colon D\to \Omega^C$, and you want to consider the property "$c\in f(d)$". This is a property of a pair $(c,d)$, where $c$ is an element of $C$ and $d$ is an element of $D$, so it should be represented by a subobject of $C\times D$.
Note that the predicate $c\in f(d)$ is obtained from the predicate $c\in A$ by substituting $(c,f(d))$ for $(c,A)$. So we form the appropriate subobject of $C\times D$ by pulling back $\in_C\hookrightarrow C\times \Omega^C$ along the arrow $\mathrm{id}_C\times f\colon C\times D\to C\times \Omega^C$, just as you did in your question:
$$\require{AMScd}
\begin{CD}
P @>>> \in_C\\
@VVpV @VVV \\
C \times D @>{\mathrm{id}_C \times f}>> C \times \Omega^C
\end{CD}$$
To finish: Given elements $c\colon 1\to C$ and $d\colon 1\to D$, what does it mean to ask whether $c\in f(d)$? Well, we just ask whether the arrow $(c,d)\colon 1\to C\times D$ factors through the subobject $p\colon P\hookrightarrow C\times D$ constructed by the pullback.
