The purpose of taking any sort of average is to come up with a number that represents the whole data set--one that is, in some sense, close to most of the points. A natural way to calculate how well a number summarizes a data set is to sum the distance from that point to all other points:
$$D_1 = \sum_i |Q_i - \hat{q}|$$
where $D_1$ is the total distance, $Q_i$ is each data point, and $\hat{q}$ is the candidate summary point. The best choice of $\hat{q}$ is the one that minimizes $D_1$. This minimum turns out to be the median of the data set, where $\hat{q}$ is less than or equal to at least half of the data points and greater than or equal to the other half (more simply, sort the data points and take the middle one). The associated spread of the data set around the median is the $D_1/N$:
$$S = \frac{1}{N}\sum_i |Q_i - \hat{q}|.$$
We divide by $N$ so taking a bigger sample doesn't make the spread bigger.
One quirk of this choice of distance is that the magnitude of extreme points don't affect the median. The median of $\{1, 2, 3, 4, 5\}$ is $3.$ The median of $\{1, 2, 3, 40, 500\}$ is also $3.$ For some purposes, this is fine [1], but for others we want extreme values to affect the average.
To make sure extreme points affect the average, we choose another metric: the squared distance.
$$D_2 = \sum_i \left(Q_i - \bar{q}\right)^2$$
Squaring the distance magnifies the influence of values that are far from $\hat{q}$. The minimum of $D_2$ occurs when $\bar{q}$ is the mean of the data set.
$$\bar{q} = \frac{1}{N}\sum_i Q_i.$$
We once again define the spread as $D_2/N.$
$$\sigma^2 = \frac{1}{N}\sum_i \left(Q_i - \bar{q}\right)^2$$
But, this quantity has units of $Q^2,$ which often isn't easily comparable to $Q$ and makes it impossible to write $Q = \bar{q} \pm \sigma^2.$ So, we define the spread around the mean as the square root of this value:
$$\sigma = \sqrt{\sigma^2} = \sqrt{\frac{1}{N}\sum_i \left(Q_i - \bar{q}\right)^2}.$$
Now we can write $Q = \bar{q} \pm \sigma.$
In summary, the formula for the deviation comes from our choice of how to calculate the quality of the average value of a data set. That choice of quality calculation then determines how the average itself is calculated.
[1] One example of using the median instead of the mean is in economic data. Calculating the mean wealth of a population can be misleading due to a few extremely rich persons dragging the mean upwards. If a sample of 10 has one person with a million dollars and nine with nothing, the mean wealth is \$100,000. This sounds like well-off sample. The median, however, is \$0, which gives a better picture of how well-off these people are.