Credit risk arises from the failure of counterparty to fulfill its contractual obligations. Credit risk can arise both from loan portfolio and investment portfolio.
In case of loan portfolio, the risk that bank carries is that of default by the borrower of the loan. If the borrower defaults to repay the entire or some part or the loan then bank suffer loss of the amount that the borrower is unable to repay. This situation can arise from any unexpected event or accident or the borrower may just refuse to repay the loan amount. Higher risk comes with lower credit rating of the borrower and banks charge higher interest rate in order to compensate the higher risk, though the credit risk remains the same.
In case of investments the bank invests in bonds, the issuer of bonds may default leading to loss for the bank. In case of transactions in securities or derivatives, bank suffers credit risk if counterparty has to pay the settlement amount to the bank and defaults on the same. The credit risk can arise for different swap instruments (like interest rate swap, currency swap etc.) as well where the counter party default to pay the settlement amount as per the contractual obligation.
Credit risk is calculated based on the credit rating of the borrower or the counter party. Some banks have made it mandatory to check the credit rating of the counter party before going into any contractual agreement with the same.