If one component of shape is the special value -1, the size of that dimension is computed so that the total size remains constant. In particular, the shape of [-1] flattens into 1-D. One component of shape can be -1. Basically, it means - I do not have time to calculate all the dimensions, so infer the one for me. In your case because x * 28 * 28 * 1 = 784 so your -1 = 1

now coming to your second question, x is being reshaped because they are planning to use convolution for image classification. So they need to use some spatial information. Current data is 1 dimensional. So they transform it into 4 dimensions. the 4th dimension is the batch. Now, why __4 dimensions__?

TensorFlow’s convolutional conv2d operation expects a 4-dimensional tensor with dimensions corresponding to batch, width, height, and channel.

[batch, in_height, in_width, in_channels]

If you wish to know more about TensorFlow then visit this TensorFlow Tutorial.