Even where the police don’t arrest you, they are allowed to detain you for investigative purposes if there are reasonable grounds to suspect that you are connected to a particular crime and the detention is necessary. (1)

If police detain you, they are also allowed to search you, if the search is reasonably necessary in the circumstances. The circumstances include the seriousness of the offence and any other information they know about the suspect, or the crime.

The higher the risk associated with the crime being investigated, the more ability the police have to detain you. They do not have to wait for harm to materialize.  So, for example, if the police receive a report of men in black jackets carrying guns outside a nightclub, they can stop all men outside that nightclub to search for weapons, because the risk of harm from gunfire is high. (2)

Police are not allowed to detain you on the basis of a vague or non-existent concern for safety, or search you on the basis of a hunch or mere intuition.
In all cases, the investigative detention must be brief. You have a right to counsel as soon as you are detained, even if the police do not tell you immediately about your right to counsel.

If You Are Stopped by Police for Investigative Purposes

If you are stopped by police for investigative purposes, be polite. If you do not want to answer questions, tell the police that you do not want to answer questions until you get legal advice.
You do not have to answer questions except in the circumstance listed here.

During an investigative detention, the police can do a pat-down search for weapons. If they feel an object under your clothes, they can search for the object. If the police search you, it is not a good idea to physically resist, but you can say that you do not consent.

If you are stopped at a roadside alcohol check, the police can ask you to do a sobriety test and give a breath sample.

(1) R. v. Mann 2004 SCC 52

(2) R. v. Clayton 2007 SCC 32