Under GDPR there are 6 grounds based on which anybody can process personal data. Those are:
Consent
You explicitly agreeing to it. This needs to be opt-in, informed, specific and freely given, but also gives the greatest freedom to a company.
Contract
This is the basis which raj's answer confused with legitimate interests. This is the processing that is required to fulfil a contractual obligation (note that contracts do not always need to be signed, e.g. an order from an eshop).
need to process someone’s personal data:
to deliver a contractual service to them; or
because they have asked you to do something before entering into a contract (eg provide a quote).
Legitimate interests are the most flexible lawful basis for processing personal data. In the words of the UK's ICO 1:
It is likely to be most appropriate where you use people’s data in ways they would reasonably expect and which have a minimal privacy impact, or where there is a compelling justification for the processing.
The underlying text from the GDPR itself (definitions and links added are mine)
processing is necessary for the purposes [=a specific minimal type of processing] of the legitimate interests pursued by the controller [=the company wanting to process your data] or by a third party except where such interests are overridden by the interests or fundamental rights and freedoms of the data subject [=you] which require protection of personal data, in particular where the data subject is a child.
Source: GDPR Article 6(1f)
So basically a legitimate interest claim by a company is them saying 'we are convinced that our interest outweigh the negligible impact on the privacy of the people whose data we process'. This doesn't give them a free pass though, as GDPR also gives the right to object
The data subject shall have the right to object, on grounds relating to his or her particular situation, at any time to processing of personal data concerning him or her which is based on point [public interest] or [legitimate interest] of Article 6(1), including profiling based on those provisions.
Source: GDPR Article 21(1)
Which then require the company to either concede and stop the processing or justify their claim. Companies in practise have taken this to mean they can basically just do a bunch of processing and as long as they make the objection process (=opt-out) easy enough the theory is that they will get away with it.
Notes:
1 The UK left the EU, but they still have by far the best English language resource explaining GDPR and for the time being "UK GDPR" matches "EU GDPR" one on one as far as I am aware
Legitimate interest makes complete sense with something like an online shop, but trying to read a news article/blog post, do I really need to have 100s of vendors claiming "legitimate interest"?