There are a variety of reasons for the high cost of some chemicals.
The first is the availability or rarity of the chemical. For an example: if a company is selling 2g of a chemical which only has 10g worldwide it is going to be much more expensive then 2g of a chemical which has 100,000g worldwide. This is of course and extreme example but you get the idea.
The second way that a chemicals cost can balloon is the cost to manufacture. For an example: lets say that I am a company manufacturing X in New Jersey however X needs raw materials that are only found 1KM beneath the surface of Iceland. This means that I first need to set up a mining operation in Iceland and then transport the raw materials to New Jersey before I can begin to create X. This is an example of high costs for the manufacturers which inevitably get passed on to the consumers.
This explains the expensive chemicals and as for the inexpensive chemicals it is the exact opposite from the aforementioned. i.e. high availability and low production costs.