UserInfo.getOrganizationId() can be used to fetch the Salesforce organization id using Apex.
Sample Code:
Id OrgId = UserInfo.getOrganizationId();
System.debug(
'Org Id is ' +
OrgId
);
UserInfo.getOrganizationId() allows us to programmatically get our Salesforce Organization Id.