Cross-correlation of two signals using matlab
xcorr function (of Matlab) computes the cross-correlation between two sequences $x[n]$ and $y[n]$ of length $M$ each. If $x$ and $y$ are random processes (jointly WSS) then xcorr returns an estimate $$\hat{r}_{xy}[m] = \frac{1}{M} \sum_{n=0}^{M-1} x[n]y[n+m]^*$$ computed over the two random vectors $x$ and $y$, of the theoretical cross-correlation between the two random processes $X$ and $Y$:
$$r_{xy} [m] = E\{ X[n]Y[n+m]^* \} $$ (Note that the practical estimate computation (the sum's range and the scale) may be modified to make it an unbiased estimator and to deal with out of bound entries...)

When the two sequences belong to deterministic signals, then it's called as the deterministic cross-correlation, essentially the same thing computed from a practical point of view, but its interpretation is made carefully depending on the case.

In signal processing theory and signal detection theory, the operations of correlation, convolution and matched filtering merge at some point. Without further details, one can observe that given two sequences $x$ and $y$, the the computation mechanics and hence the results of the three operations, will be the same (with slight modifications in the orientation of the arguments) 

Matched filtering, essentially convolves a test signal with a target signal, with the test signal reversed in time. It can be shown that there'll be a unique peak at the matched filter output, if the target signal involves a copy of the test signal inside. From a theoretical point of view, intuition tells that, if the target and test signals are uncorrelated (and orthogonal, for zero mean processes, as a result) their cross-correlations should produce low values in line with inner products between orthogonal vectors. But when the target signal involves a copy of the test signal at some point, then there will be high correlation between the target and test signals close to those shifts, at the output, that will create the peak.

There are subtle details, however, such as if the test signal is contained more than once, if several test signals overlapp, and if the test signal is not contained purely but through some (inevitable) transformations, which make the comparison more difficult or less reliable unless those transformations are known and rolled back properly.

In most practical scenarios, the target signal will consist of noise  or irrelevant signals plus the test signal. And for those lags which compare the test signal with the noise portions, the output will be arbitrary (in the ideal case of when the noise belongs to a zero mean white random process, the output should tend to zero, as the summation would sequentially add and subtract test signal samples which would produce apprx a zero, when the test signal belongs to a zero mean (ergodic) process). And when the comparison is between target signal and test signal for a lag that corresponds to the place of the test signal in the target signal, the computation should produce a large value, as the sum is essentially computing the energy of the test signal; i.i, sum of $x[n]^2$ values.

Note that when the SNR is low; that the noise portions have large amplitudes compared to the test signal embedded in the target signal, then the above paragraph becomes invalidated and the expected peak is burried within random peaks produced as the result of the irrelevant computations between the test signals and noise contained in the target signal.