The problem with these terms is that they are used differently in different situations.
The official definition among linguists goes like this: two versions of language are dialects of the same language if they are mutually intelligible. In other words, if people speaking these forms can understand each other, they are dialects of the same language. Languages, then, are mutually unintelligible.
There are two main problems with this definition, though. First, it's difficult to define mutual intelligibility, and second, these terms are used differently for sociopolitical reasons.
The mutual intelligibility issue is a major one. How much intelligibility counts? Is it 50%? 75%? and so forth. I'm a native speaker of English, for example, but I don't understand all dialects of English at the same level. In addition, some dialects aren't completely discrete from others. For example, in the region roughly in the west of Germany and the eastern part of the Netherlands, there is more like a continuum of dialects spoken from German to Dutch. People near the border might be able to understand both languages, but people in Germany can't understand people from the Netherlands, and vice versa.
The second problem in defining these terms is the sociopolitical one. Despite intelligibility or non-intelligibility, people want to define who speaks the same language and who doesn't. For example, Hindi and Urdu are the same language -- they are mutually intelligible -- but for sociopolitical and religious reasons they are called by different names. Likewise, there are a number of non-mutually intelligible languages spoken in China, but for political reasons, they are all called dialects of the Chinese language, to make it seem like the country is more culturally unified than it is. Finally, the term "dialect" is used in a derogatory way in places such as Germany or Italy, for example, to refer to versions of the language that people don't like. Germans, for example, will say that people in certain regions of Germany speak "German" and then their "dialect". But actually, both of those are dialects of German.
You are also right that there can often be major differences between written and spoken languages. In general, linguists are mostly interested in spoken language, because written language is somewhat external (you can still be a native speaker of a language and not know how to write, for example). However, it is true that there are some major differences between spoken and written versions of some languages. Arabic and French are two languages that spring to mind.
In order to get around the problem of defining language and dialect from each other, linguists often refer to "variety", meaning either a language or a dialect.
I'm a linguist.