I'm really confused by the definition and uses of operators in quantum mechanics. Usually we say that the state of a system is described by some vector $\lvert\psi\rangle$ in a Hilbert space $H$, and then we define operators acting on said vector, for example $\hat{p}: H\rightarrow H$. But often I read things like $$ \hat{p}\psi(x)=-i\hbar\frac{\partial}{\partial x}\psi(x)$$
I don't understand. $\psi(x)=\langle x\rvert\psi\rangle$ is a function in $L^2$ or some other space, not the same Hilbert space as $\lvert\psi\rangle$. More precisely $\psi(x)=\langle x\rvert\psi\rangle$ is an element of the field associated with $H$ for fixed $x$, I don't understand how can we apply $\hat{p}$ to this object.
How should I interpret this?
EDIT: I just realized that my question is a duplicate of this one, I must say that the "related" section is a much better search engine than the search engine. I have a question about ACuriousMind's answer. He writes that one can define a map $$\mathrm{Ket}: L^2(\mathbb{R},\mathbb{C})\rightarrow \mathcal{H}_{1D}, \psi \mapsto|\psi\rangle := \int_{-\infty}^\infty\psi(x)|x\rangle\mathrm{d}x $$
But I don't really understand how $$\int_{-\infty}^\infty\psi(x)|x\rangle\mathrm{d}x $$ is defined. How can one take an integral of a ket? The integral is a functional in $L^2$, not whatever space $\lvert x \rangle$ is in.