When we write down a wavepacket, we're trying to solve the following problem: Given an initial profile for our wavepacket, $\psi(x,0)$, what will it look like in the future? In other words, we want to find $\psi(x,t)$ given $\psi(x,0)$. To solve this problem, we need to have some wave equation that tells us how the wavepacket evolves in time. For different waves in different contexts, we'll have different wave equations. But we'll assume two things about the wave equation:
It is linear. This means that if $\psi_1(x,t)$ is a solution and $\psi_2(x,t)$ is a solution, then $a_1\psi_1(x,t)+a_2\psi_2(x,t)$ is a solution. Generalizing, it means that if $\psi_k(x,t)$ is a solution, so itis $\int dk\ a_k\psi_k(x,t)$.
If we start with an initial profile $\psi(x,0)=e^{ikx}$, then the solution to our wave equation is $\psi(x,t)=e^{i(kx-\omega_k t)}$, where $\omega_k$ is a constant that may depend on $k$.
You can check for yourself that generic wave equations, such as the equation for a wave on a string, or the Schrodinger equation, satisfy these properties. Now, once we know the wave equation has these properties, we can solve our problem!
First, we realize that by property (2), all functions of the form $e^{i(kx-\omega_k t)}$ are solutions to our wave equation. That means that, by property (1), any linear combination of these functions is also a solution to our wave equation. So we can write down a guess for the solution:
$$ \psi(x,t)=\frac{1}{\sqrt{2\pi}}\int dk\ g(k)e^{i(kx-\omega_kt)} $$
So far, $g(k)$ is just some unknown function. Any choice of $g(k)$ will give a solution to the wave equation, by properties (1) and (2). But we also need our solution to match with our initial profile, $\psi(x,0)$. Plugging in $t=0$ thus gives a condition on $g(k)$:
$$ \psi(x,0)=\frac{1}{\sqrt{2\pi}}\int dk\ g(k)e^{ikx} $$
By Fourier's theorem, we can then immediately find the $g(k)$.
$$ g(k)=\frac{1}{\sqrt{2\pi}}\int dx\ \psi(x,0)e^{-ikx} $$