How does testing a CNN work after training? i mean that how CNN know the output probability will lead to following class. i mean that if i train the CNN on the Dog and cat picture or data set so at the end of the know that this probability will lead to the Dog or cat...
The last layer of your neural network is a sigmoid function. During training, all the parameters are being learned. Hence, given a new picture, we can compute a value that is between $0$ and $1$ from the sigmoid function (which is used as a model of probability) to let us know the desired probability.

For multi-class classification problem. A softmax layer is used instead.