Imagine the sequence of numbers that covers the two dimensional grid as follows:

This gives you a very simple and compact way of encoding two numbers into one.
Suppose that $a$ is the row, $b$ is the column and $N$ is the cell value at coordinates $(a,b)$.

You can easily verify that the encoding formula is just:

$$
N = \frac{(a+b)^2 + 3a + b}{2}
$$

And the decoding, given N, is given by:

$$
s = \lfloor\sqrt{2N}\rfloor
$$

$$
a = \frac{2N - s^2 - s}{2}
$$
$$
b = s - a
$$

Where $s$ is just an auxiliary variable, for conveniency, and $\lfloor \rfloor$ is the floor operator, that keeps only the integer part of a real number.

Now, you can extend this idea recursively. If you want to encode 3 numbers, encode the first with the second, and then encode the result with the third, to obtain a single integer. The same generalizes to k numbers. Just keep on going.

To decode you apply a similar reasoning. If you know that you expect k numbers, perform the decoding k-1 times, successively.