"Cosine similarity measures the degree to which two vectors point in the same direction, regardless of magnitude.
When vectors point in the same direction, cosine similarity is 1; when vectors are perpendicular, cosine similarity is 0; and when vectors point in opposite directions, cosine similarity is -1. In positive space, cosine similarity is ...
The easiest way is to treat all out-of-vocabulary terms as a specific term in your matrix (i.e. "OOV").
So for instance, if my training data contains 3 words: "I", "like", "cake", my document-term matrix would contain 4 items, "I", "like", "cake", and "OOV".