It's an accent when the sounds change, but the language itself stays the same. For example, in Boston, the sound of the letter "R" in some words is so subtle that is almost absent. In particular, "ar" just sounds like "ah." A native of Arlington, Massachusetts would say "In Aahlington, it's haahrd to paahk a caah." A native of Arlington, Virginia would say "In Arlington, it's hard to park a car." But the words, the grammar, and most of the vocabulary are the same.
A "foreign accent" is often the result of a speaker using the same words and grammar as a native speaker, but using the vowel and consonant sounds of their native language. For example, the English "th" sound is unusual and hard for speakers of other languages. French speakers commonly substitute a letter that sounds to us like a "z." "Zee streets of Paris, zey are beautiful" instead of "The streets of Paris, they are beautiful."
It's a dialect when the actual structure of the language changes a little, and when the change is large enough to interfere with mutual understanding. For example, a colleague of mine once said "That needs fixed." I would have said "that needs to be fixed." The colleague was from Pittsburgh, and that grammatical structure is common in "Pennsylvania Dutch" dialect. She pronounced "needs" the same way I do, she pronounced "fixed" the same way I do, but she puts the words together in a pattern that is not standard English.