how to connect voip from to service providers companies, say Compny A & Company B....

Solution
This is a pretty standard function of most VoIP controllers more commonly called a IP-pbx. They have the ability to form peering relationships with other company ip-pbx and even use public telco line to interconnect if necessary.