What are cookies on the internet?

Cookies are small text sequences of information that are stored directly in your browser. They are mainly used to store useful things such as your login or the current contents of your shopping basket. Why is this necessary at all?