The gamma function extends the factorial function. This can be proved inductively using integration-by-parts.
But if you didn't already know that the gamma function had this property, and you wanted to construct a continuation of factorial a priori, how could you construct the gamma function? How could you motivate an exploration which would lead to this answer?
Euler was the first to discover/invent the Gamma function. His intuition was as follows: one complicated way to calculate $n!$ is as the infinite product $$\prod_{k=1}^{\infty} \frac{k}{k+n}.$$ The idea is that as $k$ tends to infinity in the denominator we get "all terms from $1+n$ till $\infty$" and in the numerator we get "all terms from $1$ till $\infty$" so that after cancellations we are left with $n!$. The beauty is that this product also makes sense for non-integer $n$.
There is a little problem with convergence though: the above product diverges to $0$. To see this, consider the logarithm of the product which gives us the series $$\sum_{k=1}^{\infty} \log\left(\frac{k}{k+n}\right)$$ and while the general term does converge to $0$, the series diverges to $-\infty$, which follows since from $\log x \sim x-1$ we see that the series should behave roughly like the (negative of the) harmonic series. To summarize, the series diverges to $-\infty$ so the product diverges to $0$.
Euler's second brilliant idea was to re-write the product instead as $$\prod_{k=1}^{\infty} \frac{k^{1-n}(k+1)^n}{k+n}=\frac{1\cdot 2^n}{1+n}\cdot\frac{2^{1-n}\cdot 3^n}{2+n}\cdot\frac{3^{1-n}\cdot4^n}{3+n}\cdots $$ where the idea is that if we multiply the numerators up to the $k$th term we get $k!\cdot (k+1)^n$, and the $(k+1)^n$ term is again going to cancel with the term after it and so on ad infinitum, so over all in the numerator we are left with "the product of all terms from $1$ to $\infty$" which is the same as we had in the initial product. This product is the first expression Euler gives for the Gamma function, in a letter to Goldbach in 1729. The advantage of this form is simply that the infinite product now converges.
Here we already have an extension of the factorial function to non-integer values. For instance, Euler is able to find $(\frac{1}{2})!=\frac{\sqrt{\pi}}{2}$ by substituting $n=\frac{1}{2}$ into the product and utilizing the formula $$\frac{\pi}{4}=\frac{2\cdot 4}{3\cdot 3}\cdot\frac{4\cdot 6}{5\cdot 5}\cdot\frac{6\cdot 8}{7\cdot 7}\cdots$$ which was found by Wallis some decades earlier.
It is speculated that this value is what made Euler think that it should be possible to express this infinite product in terms of an integral (as $\pi$ is related to areas, and is ubiquitous in the result of many definite integrals).
For this purpose Euler started by looking at $\int_0^1 x^a (1-x)^n dx$ where $n$ is an integer and $a$ is an arbitrary constant. Expanding $(1-x)^n$ by the binomial theorem gives that the integral equals $\frac{n!}{\prod_{k=1}^{n+1} a+k}$. Special cases of this integral were discussed by Wallis, by Newton, and by Stirling, so it stands to reason that Euler was familiar with this integral and chose it as a starting point since evaluating it gives an expression which has $n!$ in it. Some ingenious manipulations on this integral allowed Euler to express $n!$ itself as an integral, the final result being that $n! = \int_0^1 (-\log x)^n dx$. From here it is a simple substitution to get to the familiar modern definition of the Gamma function.
This answer is mostly based on the great How Euler Did It - Gamma the function by Ed Sandifer and the references there, specifically the reference [D] which elaborates on the manipulations required to get to $\int_0^1 (-\log x)^n dx$ (pages 853-855). For a lot more detail check out that article and the references.