You're getting a number of pedantic responses where people are trying to call you out for lumping together English accents, but in this instance it's uncalled for. You could have worded your question something like "Why does the UK have such a large degree of dialect diversity given its small area" and the meaning would be identical.
Here's an actual answer –
In Historical Linguistics there's a well-established notion that the place in which the greatest degree of variation (of a single group of related speech varieties) exists is the place which is most likely to be the homeland of that group. In other words, places with a crap-ton of dialects in close proximity are places where that language has been spoken longer than places which don't show that much diversity. You could easily glance at the US then glance at England and immediately be able to say with confidence that English has been spoken in England longer than in America.
The longer a language is spoken somewhere, the more different the dialects will become. There are of course other factors involved, but this idea is a consistent factor in diversification of dialects, and the other factors (e.g. immigration patterns, conquests) ultimately are just contributing to the first point. So any given dialect in Scotland will be more distinctly different from any given dialect in the south of England as compared to any two dialects in an equivalent area in Canada for the simple reason that those dialects have been developing for a much longer time in Europe than in the Americas.
There are exceptions to the "greatest diversity = homeland" idea, such as if an entirely language group is displaced by war or something else. You see this a lot in Asia where pretty much every Southeast Asian language really came from China but people were pushed south constantly throughout the history of the region. I'm exaggerating a little there, but not much. Thai, Vietnamese, Hmong etc, are all traceable back to what's now China, and to some degree still exist there in one form or another.
So that's what you're seeing in the UK. More time = more dialects in the same amount of space.
Hi! hopefully someone can drop by with a nice overview for you, but until then, there have been several posts asking about languages in Scotland that may be interesting. Check them out; I think all of these posts are locked now, so if you have follow-up questions, ask them here & include the relevant user's username to auto-page them.
When did people living in modern day Scotland start speaking English? - includes a quick overview of language in Scotland, and links to several more posts
A question about Scottish accents... - some discussion, and links to the same posts as above
a few questions on the 18th c specifically:
Are they so different? Are they more different than places seperated by the Mason Dixie line? (this is a question, I'm not trying to be difficult..)