Having a medium of exchange makes life easier than under barter economy and societies have always organised themselves around the best monetary standard they could find. Until industrialisation of the paper printing process, that happened to be gold, which is small, malleable, portable and with no tendency to tarnish. Crucially, it's also relatively finite and this particular characteristic (in combination with the others) can be very useful in environments characterised by monetary mischief.
I view it primarily as insurance against such environments. It’s a lump of metal with no cash flows and no earnings power. In a very real sense it's not intrinsically worth anything. If you buy it, you're forgoing dividend or interest income and the gradual accumulation over time of intrinsic value since a lump of cold, industrially useless metal can offer none of these things. That forgone accumulation of wealth is like the insurance premium paid for a policy which will pay out in the event of an extreme inflation event.
Is there anything else which will do that? Some argue that equities hedge against inflation because they are a claim on real assets, but most of the great bear market troughs of the 20th century occurred during inflationary periods. A more obvious inflation hedge is inflation linked bonds, but governments can default on these too. More exotic insurance products like sovereign CDSs, inflation caps, long-dated swaptions or upside yield curve volatility all have their intuitive merits. But they all come with counterparty risk. Physical gold doesn’t. Indeed, during the “6000 year gold bubble” no one has defaulted on gold. It is the one insurance policy which will pay out when you really need it to.
There is nothing mystical about gold and I don't consider myself a gold bug. In fact, I'm not sure I'd even classify gold as an ‘investment’ in the strictest sense of the word. Well chosen equities (not indices) will act as wealth-compounding machines and are likely to make many times the initial outlay in real terms over time. These are ‘investments’ because so long as the economics of each business remain firm, you don’t want to sell. As they say in the textbooks, you ‘buy to hold.’ But gold isn't like that. Like all commodities, it's intrinsically speculative because you only buy it to sell it in the future.
The reason I own gold is because I'm worried about the long-term solvency of developed market governments. I know that Milton Friedman popularised the idea that inflation is “always and everywhere a monetary phenomenon” but if you look back through time at inflationary crises - from ancient Rome, to Ming China, to revolutionary France and America or to Weimar Germany - you'll find that uncontrolled inflations are caused by overleveraged governments which resorted to printing as the easiest way to avoid explicit default (whereas inflation is merely an implicit default). It’s all very well for economists to point out that the cure for runaway inflation is simply a contraction of the money supply. It’s just that when you look at inflationary episodes you find that such monetary contractions haven't been politically viable courses of action.
Economists, we find, generally don’t understand this because economists look down on disciplines which might teach them it, such as history, because they aren’t mathematical enough. True, historians don’t use maths (primarily because they don’t have physics envy) but what they do use is common sense, and an understanding that while the economic laws might hold in the long run, in the short run the political beast must be fed.