I decided I was going to be really good and cycle to the gym, do a workout and then cycle home. I got half way there and when I was going up this super big hill and it made this clicking noise, now the cogs on the back wheel have come loose and spin as they are not attached.
Does anyone have any ideas what has happened or how to fix it?
You need a new 'block' on the back wheel. You can buy them and fit them yourself. However, there are so many different types requiring different tools it isn't worth the hassle.
Take it to a decent bike shop - last time I had one replaced it was around �30 and about 10 minutes work.
If your bike hasnt been used for ages the pawl (a little bit of metal on a spring that lets the bike freewheel) inside the cogs can get stuck. Before replacing the whole block, lie it down, give it a good squirt with WD40, and leave for a while to soak in. Occasionally this will work, though a new freewheel with cogs will not break the bank.
do as stubbings says but if no joy also try tapping it lightly, ( empasise lightly! no sledge hammers.) to jar the pawl & springs loose. after the wd40.