Here is the complete solution to the problem including some special cases for an easy start.
With analogy to the integrating factor method from ODEs it seems natural to rearrange
\begin{align*}
\mathrm{d}X_t = (a(t)X_t+ b(t)) \mathrm{d}t + (g(t)X_t+ h(t))\mathrm{d}B_t
\end{align*}
to the form
\begin{align*}\mathrm{d}X_t - X_t \left( a(t)\mathrm{d}t+g(t)\mathrm{d}B_t\right) = b(t) \mathrm{d}t + h(t)\mathrm{d}B_t .\end{align*}
Now we want to find a "nice" stochastic process $Z_t$ such that
\begin{align*}( \star) \ \ \ \mathrm{d}(X_tZ_t) =& Z_t\mathrm{d}X_t \underbrace{- Z_tX_t \left( a(t)\mathrm{d}t+g(t)\mathrm{d}B_t\right)}_{X_t\mathrm{d}Z_t + \mathrm{d}X_t\mathrm{d}Z_t} \\ =& Z_t(b(t)\mathrm{d}t + h(t)\mathrm{d}B_t).\end{align*}
Assume that $Z_t$ is an Itô process such that
$$\mathrm{d}Z_t = f_1(t, Z_t)\mathrm{d}t + f_2(t, Z_t)\mathrm{d}B_t , \ \ Z_0 = 1.$$
Let us apply Itô's product formula to $\mathrm{d}(X_tZ_t)$ we obtain that
\begin{align*}\mathrm{d}(X_tZ_t) =& Z_t \mathrm{d}X_t + X_t \mathrm{d}Z_t + \mathrm{d}X_t \mathrm{d}Z_t \\ ( \star \star) \ \ \ \ \qquad = & Z_t\mathrm{d}X_t + X_t \left( f_1(t, Z_t)\mathrm{d}t + f_2(t, Z_t)\mathrm{d}B_t \right) + (g(t)X_t + h(t))f_2(t, Z_t) \mathrm{d}t.\end{align*}
Comparing the above with the right hand-side of $(\star)$ we arrive at
\begin{align*}- Z_tX_t \left( a(t)\mathrm{d}t+g(t)\mathrm{d}B_t\right) = X_t \left( f_1(t, Z_t)\mathrm{d}t + f_2(t, Z_t)\mathrm{d}B_t \right) + (g(t)X_t + h(t))f_2(t, Z_t) \mathrm{d}t \end{align*}
and thus
\begin{align*} -Z_tX_tg(t)\mathrm{d}B_t &= X_t f_2(t, Z_t)\mathrm{d}B_t \\
-Z_tX_ta(t)\mathrm{d}t &= \big(X_tf_1(t, Z_t)+(X(t)g(t)+h(t))f_2(t,Z_t)\big)\mathrm{d}t.\end{align*}
From the first equation we can deduce that $f_2(t, Z_t) = -Z_tg(t)$ and so the second one converts to
$$-Z_tX_ta(t)\mathrm{d}t = \big(X_tf_1(t, Z_t)-Z_tg(t)(X(t)g(t)+h(t))\big)\mathrm{d}t,$$
and so
$$ Z_t (-X_ta(t)+X(t)g^2(t)+g(t)h(t)) \mathrm{d}t = X_tf_1(t, Z_t)\mathrm{d}t.$$
Hence,
$$f_1(t, Z_t) = Z_t(-a(t)+g^2(t)+X_t^{-1}g(t)h(t)).$$
However, we want $Z_t$ to be free of $X_t$ for that we consider two cases
CASE 1. $g(t) \neq 0$, $h(t)=0$
Then $Z_t$ would satisfy
$$\mathrm{d}Z_t = Z_t(-a(t)+g^2(t))\mathrm{d}t -Z_t g(t)\mathrm{d}B_t , \ \ Z_0 = 1$$
(We solve the above SDE by a standard trick involving Ito's formula, that is, first we divide by $Z_t$ to obtain the expression for $Z_t^{-1}\mathrm{d}Z_t$ and then derive the expression $\mathrm{d}(\ln(Z_t))$)
and so $$Z_t = \exp\left( \int_0^t\left( \frac{1}{2}g^2(s) - a(s)\right) \mathrm{d}s - \int_0^t g(s)\mathrm{d}B_s\right).$$
This is the integrating factor which you obtained, and it is not the correct one if $h(t) \neq 0$.
If we continue then we obtain
\begin{align*}\mathrm{d}(Z_tX_t) =& Z_tb(t)\mathrm{d}t
\\ Z_tX_t =& X_0 + \int_0^t Z_sb(s) \mathrm{d}s\\
X_t =& X_0Z_t^{-1}+ Z_t^{-1}\int_0^t Z_sb(s) \mathrm{d}s\\
X_t =& \exp\left( \int_0^t\left( a(s) - \frac{1}{2}g^2(s) \right) \mathrm{d}s + \int_0^t g(s)\mathrm{d}B_s\right) \\ &\cdot \left(X_0+ \int_0^{t} b(s)\exp\left( \int_0^s\left( \frac{1}{2}g^2(r) - a(r)\right) \mathrm{d}r - \int_0^s g(r)\mathrm{d}B_r\right)\mathrm{d}s\right).
\end{align*}
CASE 2. $g(t) = 0$, $h(t) \neq 0$
Then $(Z_t)$ satisfies
$$\mathrm{d}Z_t = -a(t)Z_t\mathrm{d}t, \ \quad Z_0 =1 $$
and so
$$Z_t = \exp\left( -\int_0^t a(s)\mathrm{d}s\right).$$
Therefore,
\begin{align*}\mathrm{d}(X_tZ_t) =& Z_tb(t)\mathrm{d}t+ Z_t h(t)\mathrm{d}B_t
\\ X_tZ_t =& X_0 + \int_0^t Z_sb(s) \mathrm{d}s + \int_0^t Z_sb(s) \mathrm{d}B_s\\
X_t = & X_0Z_t^{-1}+ Z_t^{-1}\left( \int_0^t Z_sb(s) \mathrm{d}s + \int_0^t Z_sb(s) \mathrm{d}B_s\right) \\
X_t = & X_0 e^{ \int_0^t a(s)\mathrm{d}s}\\ &+ e^{ \int_0^t a(s)\mathrm{d}s}\left( \int_0^t e^{ -\int_0^s a(r)\mathrm{d}r}b(s) \mathrm{d}s + \int_0^t e^{ -\int_0^s a(r)\mathrm{d}r}b(s) \mathrm{d}B_s\right).
\end{align*}
The general case
So far we were not able to find the solution for the general case, to find it we need to modify and rearrange our initial equation in a slightly different way.
Let write the SDE for $(X_t)$ as follows
\begin{align*}\mathrm{d}X_t = (a(t)X_t + b(t)+g(t)h(t) - g(t)h(t))\mathrm{d}t + (g(t)X_t+h(t))\mathrm{d}B_t
\end{align*}
after rearranging we have
\begin{align*}\mathrm{d}X_t - \left((a(t)X_t+g(t)h(t))\mathrm{d}t+ g(t)X_t\mathrm{d}B_t\right)= (b(t)- g(t)h(t))\mathrm{d}t + h(t)\mathrm{d}B_t
\end{align*}
Now let ($Z_t$) be an Ito process that satisfies
$$\mathrm{d}Z_t = f_1(t, Z_t)\mathrm{d}t + f_2(t, Z_t)\mathrm{d}B_t , \ \ Z_0 = 1.$$
After multiplying our SDE by $Z_t$ we obtain
\begin{align*}Z_t\mathrm{d}X_t - Z_t\left((a(t)X_t+g(t)h(t))\mathrm{d}t+ g(t)X_t\mathrm{d}B_t\right)= Z_t \left((b(t)- g(t)h(t))\mathrm{d}t + h(t)\mathrm{d}B_t\right)
\end{align*}
Applying Ito product formula to $X_tZ_t$ we get
$$ \mathrm{d}(X_tZ_t) = Z_t\mathrm{d}X_t + X_t\mathrm{d}Z_t+ \mathrm{d}X_t\mathrm{d}Z_t,$$
we want to have
\begin{align*}X_t\mathrm{d}Z_t+ \mathrm{d}X_t\mathrm{d}Z_t = - Z_t\left((a(t)X_t+g(t)h(t))\mathrm{d}t+ g(t)X_t\mathrm{d}B_t\right)\end{align*}
The LHS equals to
\begin{align*}X_t\left(f_1(t, Z_t)\mathrm{d}t + f_2(t, Z_t)\mathrm{d}B_t \right)+ (g(t)X_t+h(t))f_2(t, Z_t)\mathrm{d}t.\end{align*}
We compare with the RHS
\begin{align*}X_t\left(f_1(t, Z_t)\mathrm{d}t + f_2(t, Z_t)\mathrm{d}B_t \right)+ (g(t)X_t+h(t))f_2(t, Z_t)\mathrm{d}t = - Z_t\left((a(t)X_t+g(t)h(t))\mathrm{d}t+ g(t)X_t\mathrm{d}B_t\right)\end{align*}
and conclude that $f_2(t, Z_t) = -g(t)Z_t$. Now
\begin{align*}X_t\left(f_1(t, Z_t)\mathrm{d}t -g(t)Z_t\mathrm{d}B_t \right)- (g(t)X_t+h(t))g(t)Z_t\mathrm{d}t = - Z_t\left((a(t)X_t+g(t)h(t))\mathrm{d}t+ g(t)X_t\mathrm{d}B_t\right)\end{align*}
simplifies to
\begin{align*}X_tf_1(t, Z_t)\mathrm{d}t -g(t)^2Z_tX_t\mathrm{d}t = - a(t)Z_tX_t\mathrm{d}t\end{align*}
and so
\begin{align*}X_tf_1(t, Z_t)\mathrm{d}t = X_t\left( (g(t)^2- a(t))Z_t\right)\mathrm{d}t\end{align*}
let us conclude that
$$ f_1(Z_t, t) = (-a(t)+g(t)^2)Z_t.$$
Thus $$\mathrm{d}Z_t = (-a(t)+g^2(t))Z_t \mathrm{d}t - g(t)Z_t\mathrm{d}B_t,$$
you can see the explicit form of $Z_t$ in Case 1 and
$$\mathrm{d}(Z_tX_t) = (b(t)-h(t)g(t))Z_t\mathrm{d}t + h(t)Z_t\mathrm{d}B_t.$$
Using Ito's lemma we can show that $(Y_t):= (Z_t^{-1})$ is an Ito process such that it satisfies
$$ \mathrm{d}Y_t = a(t)Y_t \mathrm{d}t + g(t)Y_t\mathrm{d}B_t,\ \ Y_0 = 1$$
it has an explicit form
$$ Y_t = \exp\left(\int_0^t (a(s)-\frac{1}{2}g^2(s)) \mathrm{d}s +\int_0^t g(s)\mathrm{d}B_s\right).$$
Finally, we see that
\begin{align*}\mathrm{d}(Z_tX_t) =& (b(t)-h(t)g(t))Z_t\mathrm{d}t + h(t)Z_t\mathrm{d}B_t \end{align*}
yields
\begin{align*}Z_tX_t =& Z_0X_0 + \int_0^t (b(s)-h(s)g(s))Z_s\mathrm{d}s + \int_0^t h(s)Z_s\mathrm{d}B_s \end{align*}
Hence, we have the explicit solution
\begin{align*}X_t =& Z_0X_0Y_t + Y_t\left(\int_0^t (b(s)-h(s)g(s))Z_s\mathrm{d}s + \int_0^t h(s)Z_s\mathrm{d}B_s\right). \end{align*}
$$\frac{dY_t}{dt}= \left\{ \left(-1+\frac{1}{2}t-\frac{\beta-1}{t}\right) + (\beta - 1) e^{\alpha B_t-\frac{\alpha^2}{2}t}\frac{Y_t}{t} \right\} Y_t$$
Looks like a Bernouilli's equation of the form
$$y'+\alpha(t)y=\beta (t) y^n$$
Here you have $n=2$. Divide by $Y^2(t)$ both side.
$$\frac{dY_t}{dt}\frac 1 {Y^2_t}= \left\{ \left(-1+\frac{1}{2}t-\frac{\beta-1}{t}\right)\frac 1 {Y_t} + (\beta - 1) e^{\alpha B_t-\frac{\alpha^2}{2}t}\frac{1}{t} \right\} $$
Then substitute $Z(t)=\frac 1 {Y_t}, Z'(t)=-\frac {Y'_t} {Y^2_t}$.
$$Z'(t)+ \left(-1+\frac{1}{2}t-\frac{\beta-1}{t}\right)Z(t)= -\left\{ (\beta - 1) e^{\alpha B_t-\frac{\alpha^2}{2}t}\frac{1}{t} \right\} $$
The equation is now a first order linear DE. Use any techniques you know to solve it ( integrating factor ). But it's not going to be easy to integrate not because of the DE but because od the functions that are really complicated.
Best Answer
Define $$\begin{align} &Z_t =\frac{1}{X_t}\\ &M_t = \exp\left\{(rK-\frac12\alpha^2)t+\alpha B_t\right\} \end{align}$$ By using the Ito's lemma, we have: $$\begin{align} &\frac{dZ_t}{Z_t} =\left(\alpha^2-rK+r\frac{1}{Z_t}\right)dt-\alpha dB_t\\ &\frac{dM_t}{M_t}= rKdt+\beta dB_t\\ \end{align}$$ Then $$\begin{align} d(M_tZ_t)&=M_tdZ_t+Z_tdM_t+\frac{1}{2}\cdot2\cdot d\langle M,Z\rangle _t\\ &=M_tZ_t\left(\frac{dZ_t}{Z_t}+ \frac{dM_t}{M_t} +\frac{\langle M,Z\rangle _t}{M_tZ_t}\right)\\ &=M_tZ_t\left( \left(\left(\alpha^2-rK+r\frac{1}{Z_t}\right)+rK-\alpha^2 \right) dt +\left(-\alpha+\alpha\right)dB_t \right)\\ &=rM_tdt\\ \end{align}$$ $$\begin{align}&\implies M_tZ_t = M_0Z_0 +r\int_0^t M_sds = x_0^{-1} + r\int_0^t M_sds\\ &\implies X_t = \frac{1}{Z_t} =\color{red}{ \frac{M_t}{ x_0^{-1} + r\int_0^t M_sds}}\end{align}$$ Q.E.D