Is it necessary to book bus tickets from Gdansk to Kaliningrad in advance, or is it enough to buy tickets at the station just before the departure? If they need to be booked in advance, where would you do it? Online with the company or locally in the company's office in Gdansk, supposing that they have one?
In most cases you can buy tickets before departure. But there can be problems during russian holidays.
At least 4 dayly buses are available from different companies.
And only one company offers you online booking: http://ecolines.net/
According to a post on TripAdvisor by JeenaB who is from Kaliningrad tickets are only available at the station. There are 3 buses, one at 6:30am, 3:30pm, and 5:00pm. JeenaB states that the buses are rarely full so there shouldn't be anything to worry about. So it looks like the only way to get a ticket is at the bus station, but there shouldn't be any trouble getting one.
You can also see a time table of both buses and the train on Konigsberg.ru, this link also has contact information for the buses and the train. Hope this answer helps! Safe travels!
Regarding trains, please note that they won't run in 2013:
[PL] [RU] We hear that the Gdynia - Malbork - Kaliningrad seasonal summer service will not run this year (Cooks Table 1038).
[PL] [RU] We hear that the Gdynia - Malbork - Kaliningrad seasonal summer service will not run this year (Cooks Table 1038).— EuropeByRail (@EuropeByRail) April 21, 2013
To visit Kaliningrad, even passing through a land boarder from Poland or Lithuania, you'll need a Visa.
That used to be a challenge (Russian visitors visas are a PAIN) but I notice that it is now possible to book an e-Visa for short visits to Kaliningrad.
Details here: http://electronic-visa.kdmid.ru/klgd_home_en.html