How long can a foreigner stay in Canada?

In one sense, as long as you can get away with it, but presuming you mean stay legally; then it depends on your status.

An ordinary tourist visa is usually good for 6 months and can be renewed under some circumstances (but you cannot work on Canada).

A student visa is typically good for as long as you are a bona-fide student (even post-grad reseach will qualify in some cases). Employment is usually restricted.

If you are a landed permanent immigrant you can stay forever (as long as you do not do something that will get you deported).

For more information contact Canada Immigration & Citizenship. They have a website.