CORS is acronym for cross origin resource sharing. Cross-origin resource sharing (CORS) is a browser mechanism which enables controlled access to resources located outside of a given origin. Many websites interact with subdomains or third-party sites in a way that requires cross-origin access. A controlled relaxation of the same-origin policy is possible using cross-origin resource sharing (CORS). Note that XMLHttpRequest and fetch follow same orgin policy and without CORS ,  scripts running on a browser client to cannot interact with resources from a different origin. 

To understand it is important to understand what is a cross origin request. Read https://clarifyforme.com/posts/5637618884673536/What-is-a-cross-origin-request . Also do not confuse between same site and same origin. same site is a cookie attribute which determine how will browser handle cookies in cross site requests. Read https://clarifyforme.com/posts/5177685533786112/what-is-the-difference-between-site-and-origin 

For security reasons, the browser applies same origin policy to scripts. ie cross origin http requests from scripts are blocked. So, a web application using XMLHttpRequest or Fetch could only make HTTP requests to its own origin. (cross origin links etc are not forbidden) hence for cross origin script requests to work, CORS(cross origin resource sharing) header origin has to be part of request and this origin must be in the whitelist of destination. Hence for CORS  usually here are the steps. Lets assume xhr cross origin request is made say from script running in a.com to b.com

FAQ

Questions) Is CORS a protection against CSRF?

Answer ) No. Infact CORS is controlled relaxation of same origin policy. Poorly configured CORS lead to security risks. CSRF attack can be prevented via SameSite attribute of cookies and CSRF tokens.

Questions) Can CORS lead to xss attack?

Answer) yes, If a website trusts an origin that is vulnerable to XSS attack then an attacker could exploit the XSS to inject some JavaScript that uses CORS to retrieve sensitive information from the site that trusts the vulnerable application. 

Questions) Can a website trust an origin that is on http?

Answer) no, 

Questions) Cross origin javascript is not able to access resouce but i am able to acccess it through a program.

Answer ) CORS is a feature implemented by the browser. Hence CORS config won’t prevent non-browser stuff from successfully retrieving your resources.

Read more/References

1. read this excellent stack over flow post : https://stackoverflow.com/questions/15726423/how-does-cors-provide-at-least-some-security-to-users 

2. https://stackoverflow.com/questions/10636611/how-does-access-control-allow-origin-header-work