Given a DOI pointing to a resource, how can I programmatically know whether the access to the resource is free of charge? E.g., if the DOI points to a research paper, how to programmatically know whether it is behind a paywall?
It is probably impossible in general. The DOI is just a stupid number telling nothing specific about the content. You may decide on the so-called prefix of the DOI whether it belongs to a commercial publisher offering usually non-free content or to a publisher of free content. I am not aware of ready-made lists for this purpose, and there are prefix-owners publishing very mixed content what the licences are concerned.
You can just try to download the research papers, and when you cannot get a pdf file than conclude that you failed at a pay wall.
But this kind of conclusion may be flawed: You may use an institutional account where your institution has payed in advance, giving you a false positive (pdf, allthough in principle there exists a paywall, but just not for you and your institution).
You may get failures (false negatives) because the site you visit may have rigid measures against bots trying to download even free content.
What are looking for information on is generally called text and data mining (TDM). Most publishers provide TDM details in the metadata associated with a DOI. The detail is usually a link to a use license. You would have to catalog the licenses to know (from the link alone) if the license provided open access to the content.
For example, here is metadata for the journal article "Representational bias in phytoliths from modern soils of central North America"
that contains a link to the license and links to the data.
For more information on TDM see Crossref's useful collection of information