It's a bit ugly ... but you could do something like
;with companyNotNull as (
SELECT * FROM #Companies WHERE CompanyID = @CompanyID
),
companyNull as (
Select CompanyID FROM #CompanyGroups WHERE CompanyGroupID = @CompanyGroupID
)
SELECT *
FROM companyNotNull
WHERE @companyID is not null
UNION ALL
SELECT *
FROM companyNull
WHERE @companyID is null
Of course this replicates the if ... then ... else structure. If both values are set then the companyID takes precedence.