This is relatively standard materials, so for the details you can consult your favourite EM textbook, but I'll sketch the overview.
The problem with Ampère's law, for any kind of loop (including planar loops!) is that there are plenty of surfaces $S$ that share the same boundary $C=\partial S$, which makes the statement
$$
\oint_C \mathbf B\cdot\mathrm d\mathbf l = \mu_0 \iint_S\mathbf J\cdot \mathrm d\mathbf S
$$
a bit suspect, unless we can (a) choose a canonical surface $S$ for each curve $C$, or (b) show that the surface integral on the right-hand side is actually independent of what surface we choose.
The resolution to this is, in fact, (b): the current flow really is independent of the surface you choose. To prove this, consider two surfaces $S_1$ and $S_2$ which share the same boundary $C$, so that we want to prove that
$$
\iint_{S_1}\mathbf J\cdot \mathrm d\mathbf S
=
\iint_{S_2}\mathbf J\cdot \mathrm d\mathbf S,
$$
or, equivalently, that
$$
{\large\bigcirc}\kern-1.55em\iint_{S}\mathbf J\cdot \mathrm d\mathbf S
=
\iint_{S_1}\mathbf J\cdot \mathrm d\mathbf S
-
\iint_{S_2}\mathbf J\cdot \mathrm d\mathbf S
=0,
$$
where $S$ is the closed surface that surrounds the space between $S_1$ and $S_2$.
Now, there's a bunch of ways to prove that that integral is indeed zero, but they all boil down to this: the closed surface integral $\mathop{\vcenter{ \unicode{x222F}}}_{S}\mathbf J\cdot \mathrm d\mathbf S$ represents the net amount of charge that enters the volume between $S_1$ and $S_2$ per unit time, and for a static situation, that net amount needs to be exactly zero, or you would have a linear growth of the enclosed charge in that volume, quickly taking you out of the static situation you thought you were in.
Of course, this does mean that Ampère's law as formulated here can no longer hold without modifications in dynamic situations - and, indeed, in that case you need to extend it to the Ampère-Maxwell law, which includes an additional term in the surface integral, and which again has the property that it holds regardless of what surface $S$ you choose to integrate over.