A broker-dealer (B/D) is a license granted by the Securities and Exchange Commission (SEC) that entitles the licensee to buy and sell securities for its clients' accounts. The firm may also act as principal, or dealer, trading securities for its own inventory.
Some broker-dealers act in both capacities, depending on the circumstances of the trade or the type of security being traded. For example, your order to purchase a particular security might be filled from the firm's inventory.
That's perfectly legal, though you must be notified that it has occurred. B/Ds range in size from independent one-person offices to large brokerage firms.