Consider a single free particle of mass $m,$ moving in space under no forces. If the particle starts from the origin at $t=0$ and reaches the position $(x,y,z)$ at time $t,$ find Hamilton's characteristic function $S$ as a function of $x, y,z,t. $
In this problem, I tried to solve by taking K.E. in terms of velocity in each direction and potential energy I have taken as P.E.$= mgz$ my final equation was
$H = (p_x^2+p_y^2+p_z^2)/(2m) + mgz$
Is this correct? And why this equation doesn't contain variable $t?$
This is definitely not correct. As there are no forces, there should be no potential energy terms. All you have is the kinetic energy in the Hamiltonian. We can see from the Lagrangian (equal to the Hamiltonian in this case!) that all the momenta are conserved via Noether's Theorem, and indeed are constants. Therefore, the classical action \begin{align*} S&=\int \mathcal{L}\,dt\\ &=\int\left(\frac{p_x^2+p_y^2+p_z^2}{2m}\right)dt\\ &=\frac{\left(p_x^2+p_y^2+p_z^2\right)t}{2m}+C. \end{align*} To get to $x,y,z,$ just note that $x=v_x\,t=(p_x/m)\,t,$ and similarly for the other three variables. Solving for $p_x$ yields \begin{align*} x&=\frac{p_xt}{m}\\ \frac{xm}{t}&=p_x. \end{align*}